Wednesday, 4 December 2013

jQuery Interview Question and Answer - Part-2

Q16. Why there are two different version of jQuery library? 

Ans: jQuery library comes in 2 different versions:
  1. Production
  2. Deployment 
The production version is quite useful at development time as jQuery is open source and if you want to change something then you can make those changes in production version. But the deployment version is minified version or compressed version so it is impossible to make changes in it. Because it is compressed, so its size is very less than the production version which affects the page load time. 

Q17. What is a CDN? 

Ans: A content delivery network or content distribution network (CDN) is a large distributed system of servers deployed in multiple data centers across the Internet. The goal of a CDN is to serve content to end-users with high availability and high performance. 

Q18. Which are the popular jQuery CDN and what is the advantage of using CDN?

Ans: There are 3 popular jQuery CDNs.
  1. Google
  2. Microsoft
  3. jQuery
Advantage of using CDN. • It reduces the load from your server. • It saves bandwidth. jQuery framework will load faster from these CDN. • The most important benefit is it will be cached, if the user has visited any site which is using jQuery framework from any of these CDN 

Q19. How to load jQuery from CDN? 

Ans: Below is the code to load jQuery from all 3 CDNs. 

Code to load jQuery Framework from Google CDN 
<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

Code to load jQuery Framework from Microsoft CDN 
<script type="text/javascript"
    src="http://ajax.microsoft.com/ajax/jquery/jquery-1.9.1.min.js">
</script>

Code to load jQuery Framework from jQuery Site(EdgeCast CDN)
<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>

Q20. How to load jQuery locally when CDN fails? 

Ans: It is a good approach to always use CDN but sometimes what if the CDN is down (rare possibility though) but you never know in this world as anything can happen. Below given jQuery code checks whether jQuery is loaded from Google CDN or not, if not then it references thejQuery.js file from your folder. 

Code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
{
  document.write(unescape("%3Cscript src='Scripts/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

It first loads the jQuery from Google CDN and then check the jQuery object. If jQuery is not loaded successfully then it will references the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder. 

Q21. What are selectors in jQuery and how many types of selectors are there? 

Ans: To work with an element on the web page, first we need to find them. To find the html element in jQuery we use selectors. There are many types of selectors but basic selectors are:

  1. Name: Selects all elements which match with the given element Name.
  2. #ID: Selects a single element which matches with the given ID 
  3. Class: Selects all elements which match with the given Class. • Universal (*): Selects all elements available in a DOM.
  4. Multiple Elements E, F, G: Selects the combined results of all the specified selectors E, F 
  5. Attribute Selector: Select elements based on its attribute value. 

Q23. What are the fastest selectors in jQuery? 

Ans: ID and element selectors are the fastest selectors in jQuery. 

Q24. What are the slow selectors in jQuery?

Ans: class selectors are the slow compare to ID and element. 

Q25. How jQuery selectors are executed? 

Ans: Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class ".myCssClass" and after that it will reject all the other elements which are not in "p#elmID".

Code:
$("p#elmID .myCssClass"); 

Q26. Which is fast document.getElementByID('txtName') or $('#txtName').?

Ans: Native JavaScipt is always fast. jQuery method to select txtName "$('#txtName')" will internally makes a call todocument.getElementByID('txtName'). As jQuery is written on top of JavaScript and it internally uses JavaScript only So JavaScript is always fast.

27. Difference between $(this) and 'this' in jQuery? 

Ans: this and $(this) refers to the same element. The only difference is the way they are used. 'this' is used in traditional sense, when 'this' is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery. 

Code:
$(document).ready(function(){
    $('#spnValue').mouseover(function(){
       alert($(this).text());
  });
});

In below example, this is an object but since it is not wrapped in $(), we can't use jQuery method and use the native JavaScript to get the value of span element. 

Code:
$(document).ready(function(){
    $('#spnValue').mouseover(function(){
       alert(this.innerText);
  });
});

Q28. How do you check if an element is empty?

Ans: There are 2 ways to check if element is empty or not. We can check using ":empty" selector.

Code:
$(document).ready(function(){
if ($('#element').is(':empty')){
//Element is empty
}
});

And the second way is using the "$.trim()" method.

Code:
$(document).ready(function(){
if ($.trim($('#element').html())=="){
//Element is empty
}
});

Q29. How do you check if an element exists or not in jQuery? 

Ans: Using jQuery length property, we can ensure whether element exists or not.

Code:
$(document).ready(function(){
if ($('#element').length > 0){
//Element exists
});
});

Q30. What is the use of jquery .each() function?

Ans: The $.each() function is used to iterate over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array.


PREVIOUS                                                                                                                                          NEXT

No comments:

Post a Comment