Friday, 6 December 2013

jQuery Interview Question and Answer - Part-5

Q61. What are deferred and promise object in jQuery?

Ans: Deferred and promise are part of jQuery since version 1.5 and they help in handling asynchronous functions like Ajax.

Q62. Can we execute/run multiple Ajax request simultaneously in jQuery? If yes, then how?

Ans: Yes, it is possible to execute multiple Ajax request simultaneously or in parallel. Instead of waiting for first ajax request to complete and then issue the second request is time consuming. The better approach to speed up things would be to execute multiple ajax request simultaneously.

Using jQuery .when() method which provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events. Find out more here.

Q63. Can you call C# code-behind method using jQuery? If yes,then how?

Ans: Yes. We can call C# code-behind function via $.ajax. But for do that it is compulsory to mark the method as WebMethod.

Q64. Which is the latest version of jQuery library?

Ans: The latest version (when this post is written) of jQuery is 1.10.2 or 2.0.3. jQuery 2.x has the same API as jQuery1.x, but does not support Internet Explorer 6, 7, or 8.

Q65. Does jQuery 2.0 supports IE?

Ans: No. jQuery 2.0 has no support for IE 6, IE 7 and IE 8.

Q66. What are source maps in jQuery?

Ans: In case of jQuery, Source Map is nothing but mapping of minified version of jQuery against the un-minified version. Source map allows to debug minified version of jQuery library. Source map feature was release with jQuery1.9.

Q67. How to use migrate jQuery plugin?

Ans: with release of 1.9 version of jQuery, many deprecated methods were discarded and they are no longer available. But there are many sites in production which are still using these deprecated features and it's not possible to replace them overnight. So jQuery team provided with jQuery Migrate plugin that makes code written prior to 1.9 work with it.

So to use old/deprecated features, all you need to do is to provide reference of jQuery Migrate Plugin.

Q68. Is it possible to get value of multiple CSS properties in single statement?

Ans: Well, before jQuery 1.9 release it was not possible but one of the new feature of jQuery 1.9 was .css() multi-property getter.

Code:

var propCollection = $("#dvBox").css([ "width", "height", "backgroundColor" ]);

In this case, the propCollection will be an array and it will look something like this.

Code:
{
  width: "100px",
  height: "200px",
  backgroundColor: "#FF00FF"
}

Q69. How do you stop the currently-running animation, remove all queued animations, and complete all animations for the matched elements?

Ans: It can be done via calling .stop([clearQueue ] [, jumpToEnd ]) method and by passing both the parameters as true.

Q70. What is finish method in jQuery?

Ans: The .finish() method stops all queued animations and places the element(s) in their final state. This method was introduced in jQuery 1.9.

Q71. What is the difference between calling stop(true,true) and finish method?

Ans: The .finish() method is similar to .stop(true, true) in that it clears the queue and the current animation jumps to its end value. It differs, however, in that .finish() also causes the CSS property of all queued animations to jump to their end values, as well.

Q72. Consider a scenario where things can be done easily with javascript, would you still prefer jQuery?


Ans: No. If things can be done easily via CSS or JavaScript then You should not think about jQuery. Remember,jQuery library always comes with xx kilobyte size and there is no point of wasting bandwidth.

Q73. Can we use protocol less URL while referencing jQuery from CDNs?


Ans: Yes. Below code is completely valid.

Code:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Q74. What is the advantage of using protocol less URL while referencing jQuery from CDNs?


Ans: It is quite useful when you are moving from HTTP to HTTPS url. You need to make sure that correct protocol is used for referencing jQuery library as pages served via SSL should contain no references to content served through unencrypted connections.

"protocol-less" URL is the best way to reference third party content that’s available via both HTTP and HTTPS. When a URL’s protocol is omitted, the browser uses the underlying document’s protocol instead. Find out more here.

Q75. What is jQuery plugin and what is the advantage of using plugin?


Ans: A plug-in is piece of code written in a standard JavaScript file. These files provide useful jQuery methods which can be used along with jQuery library methods. jQuery plugins are quite useful as its piece of code which is already written by someone and re-usable, which saves your development time.

PREVIOUS                                                                                                                                 NEXT


Thursday, 5 December 2013

jQuery Interview Question and Answer - Part-4

Q46. What is the difference between event.PreventDefault and "return false"?

Ans: e.preventDefault() will prevent the default event from occurring, e.stopPropagation() will prevent the event from bubbling up and return false will do both.

Q47. What is the difference between event.stopPropagation and event.stopImmediatePropagation?


Ans: event.stopPropagation() allows other handlers on the same element to be executed, while event.stopImmediatePropagation() prevents every event from running. For example, see below jQuery code block.

Code:
$("p").click(function(event){
  event.stopImmediatePropagation();
});
$("p").click(function(event){
  // This function won't be executed
  $(this).css("background-color", "#f00");
}); 


If event.stopPropagation was used in previous example, then the next click event on p element which changes the css will fire, but in case event.stopImmediatePropagation(), the next p click event will not fire.

Q48. How to check if number is numeric while using jQuery 1.7+?

Ans: Using "isNumeric()" function which was introduced with jQuery 1.7.

Q49. How to check data type of any variable in jQuery?

Ans: Using $.type(Object) which returns the built-in JavaScript type for the object.

Q50. How do you attach a event to element which should be executed only once?

Ans: Using jQuery one() method. This attaches a handler to an event for the element. The handler is executed at most once per element. In simple terms, the attached function will be called only once.

Code:
$(document).ready(function() {
    $("#btnDummy").one("click", function() {
        alert("This will be displayed only once.");
    });
});


Q51. Can you include multiple version of jQuery? If yes, then how they are executed?


Ans: Yes. Multiple versions of jQuery can be included in same page.

Q52. In what situation you would use multiple version of jQuery and how would you include them?


Ans: Well, it is quite possible that the jQuery plugins which are used are dependent on older version but for your own jQuery code, you would like to use newer version. So because of this dependency, multiple version of jQuery may required sometimes on single page.

Below code shows how to include multiple version of jQuery.


Code:
<script type='text/javascript' src='js/jquery_1.9.1.min.js'></script>
<script type='text/javascript'>
 var $jq = jQuery.noConflict();
</script>
<script type='text/javascript' src='js/jquery_1.7.2.min.js'></script>


By this way, for your own jQuery code use "$jq", instead of "$" as "$jq" refers to jQuery 1.9.1, where "$" refers to 1.7.2.

Q53. Is it possible to hold or delay document.ready execution for sometime?


Ans: Yes, its possible. With Release of jQuery 1.6, a new method "jQuery.holdReady(hold)" was introduced. This method allows to delay the execution of document.ready() event. document.ready() event is called as soon as your DOM is ready but sometimes there is a situation when you want to load additional JavaScript or some plugins which you have referenced.

Code:
$.holdReady(true);
$.getScript("myplugin.js", function() {  
$.holdReady(false);
});

Q54. What is chaining in jQuery?

Ans: Chaining is one of the most powerful feature of jQuery. In jQuery, Chaining means to connect multiple functions, events on selectors. It makes your code short and easy to manage and it gives better performance. The chain starts from left to right. So left most will be called first and so on.

Code:
$(document).ready(function(){
    $('#dvContent').addClass('dummy');
    $('#dvContent').css('color', 'red');
    $('#dvContent').fadeIn('slow');});​

The above jQuery code sample can be re-written using chaining. See below.

Code:
$(document).ready(function(){
    $('#dvContent').addClass('dummy')
          .css('color', 'red')
          .fadeIn('slow');   
});​

Not only functions or methods, chaining also works with events in jQuery.

Q55. How does caching helps and how to use caching in jQuery?

Ans: Caching is an area which can give you awesome performance, if used properly and at the right place. While using jQuery, you should also think about caching. For example, if you are using any element in jQuery more than one time, then you must cache it. See below code.

Code:
$("#myID").css("color", "red");
//Doing some other stuff......
$("#myID").text("Error occurred!");​

Now in above jQuery code, the element with #myID is used twice but without caching. So both the times jQuery had to traverse through DOM and get the element. But if you have saved this in a variable then you just need to reference the variable. So the better way would be,


Code:
var $myElement = $("#myID").css("color", "red");
//Doing some other stuff......
$myElement.text("Error occurred!");;​
  
So now in this case, jQuery won't need to traverse through the whole DOM tree when it is used second time. So in jQuery, Caching is like saving the jQuery selector in a variable. And using the variable reference when required instead of searching through DOM again.

Q56. You get "jquery is not defined" or "$ is not defined" error. What could be the reason?


Ans: There could be many reasons for this.

You have forgot to include the reference of jQuery library and trying to access jQuery.
You have include the reference of the jQuery file, but it is after your jQuery code.
The order of the scripts is not correct. For example, if you are using any jQuery plugin and you have placed the reference of the plugin js before the jQuery library then you will face this error.

Q57. How to write browser specific code using jQuery?


Ans: Using jQuery.browser property, we can write browser specific code. This property contains flags for the useragent, read from navigator.userAgent. This property was removed in jQuery 1.9.

Q58. Can we use jQuery to make ajax request?

Ans: Yes. jQuery can be used for making ajax request.

Q59. What are various methods to make ajax request in jQuery?


Ans: Using below jQuery methods, you can make ajax calls.

load() : Load a piece of html into a container DOM
$.getJSON(): Load JSON with GET method.
$.getScript(): Load a JavaScript file.
$.get(): Use to make a GET call and play extensively with the response.
$.post(): Use to make a POST call and don't want to load the response to some container DOM.
$.ajax(): Use this to do something on XHR failures, or to specify ajax options (e.g. cache: true) on the fly.

Q60. Is there any advantage of using $.ajax() for ajax call against $.get() or $.post()?

Ans: By using jQuery post()/ jQuery get(), you always trust the response from the server and you believe it is going to be successful all the time. Well, it is certainly not a good idea to trust the response. As there can be n number of reason which may lead to failure of response.

Where jQuery.ajax() is jQuery's low-level AJAX implementation. $.get and $.post are higher-level abstractions that are often easier to understand and use, but don't offer as much functionality (such as error callbacks).




PREVIOUS                                                                                                                                 NEXT

Wednesday, 4 December 2013

jQuery Interview Question and Answer - Part-3

Q31. What is the difference between jquery.size() and jquery.length?

Ans: jQuery.size() method returns number of element in the object. But it is not preferred to use the size()method as jQuery provide .length property and which does the same thing. But the .length property is preferred because it does not have the overhead of a function call.

Q32. What is the difference between $('div') and $('<div/>') in jQuery?

Ans: $('<div/>') : This creates a new div element. However this is not added to DOM tree unless you don't append it to any DOM element.

$('div') : This selects all the div element present on the page.

Q33. What is the difference between parent() and parents() methods in jQuery?

Ans: The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.

Q34. What is the difference between eq() and get() methods in jQuery?

Ans: eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it. get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can't be used. 

Q35. How do you implement animation functionality? 

Ans: The .animate() method allows us to create animation effects on any numeric CSS property. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.

Code:
(selector).animate({styles},speed,easing,callback)

• styles: Specifies one or more CSS properties/values to animate. 
• duration: Optional. Specifies the speed of the animation. 
• easing: Optional. Specifies the speed of the element in different points of the animation. Default value is "swing". 
• callback: Optional. A function to be executed after the animation completes. Simple use of animate function is, 

Code:
$("btnClick").click(function(){ $("#dvBox").animate({height:"100px"}); }); 

Q36. How to disable jQuery animation?
Ans: Using jQuery property "jQuery.fx.off", which when set to true, disables all the jQuery animation. When this is done, all animation methods will immediately set elements to their final state when called, rather than displaying an effect.

Q37. How do you stop the currently-running animation?
Ans: Using jQuery ".stop()" method.

Q38. What is the difference between .empty(), .remove() and .detach() methods in jQuery?

Ans: All these methods .empty(), .remove() and .detach() are used for removing elements from DOM but they all are different. .empty(): This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM. .remove(): Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. .detach(): This method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

Q39. Explain .bind() vs .live() vs .delegate() vs .on() 

Ans: All these 4 jQuery methods are used for attaching events to selectors or elements. But they all are different from each other. .bind(): This is the easiest and quick method to bind events. But the issue with bind() is that it doesn't work for elements added dynamically that matches the same selector. bind() only attach events to the current elements not future element. Above that it also has performance issues when dealing with a large selection. .live(): This method overcomes the disadvantage of bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method. .delegate(): The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored and it also supports chaining. .on(): Since live was deprecated with 1.7, so new method was introduced named ".on()". This method provides all the goodness of previous 3 methods and it brings uniformity for attaching event handlers.

Q40. What is wrong with this code line "$('#myid.3').text('blah blah!!!');" 

Ans: The problem with above statement is that the selectors is having meta characters and to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar"). So the correct syntax is, $('#myid\\.3').text('blah blah!!!');

Q41. How to create clone of any object using jQuery?

Ans: jQuery provides clone() method which performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes. 

Code:
$(document).ready(function(){ $('#btnClone').click(function(){ $('#dvText').clone().appendTo('body'); return false; }); });

Q42. Does events are also copied when you clone any element in jQuery?

Ans: As explained in previous question, using clone() method, we can create clone of any element but the default implementation of the clone() method doesn't copy events unless you tell the clone() method to copy the events. The clone() method takes a parameter, if you pass true then it will copy the events as well. 

Code:
$(document).ready(function(){ $("#btnClone").bind('click', function(){ $('#dvClickme').clone(true).appendTo('body'); }); 

Q43. What is difference between prop and attr? 

Ans: attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements. Attributes carry additional information about an HTML element and come in name="value" pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties. attr() gives you the value of element as it was defines in the html on page load. It is always recommended to useprop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state.

Q44. What is event.PreventDefault?

Ans: The event.preventDefault() method stops the default action of an element from happening. For example, Prevents a link from following the URL.

Q45. What is the difference between event.PreventDefault and event.stopPropagation?

Ans: event.preventDefault(): Stops the default action of an element from happening. event.stopPropagation(): Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.

PREVIOUS                                                                                                                                 NEXT

Relationship Quotes

Don't wait forever to tell them how you feel, eventually you'll be too late :P

Relationship require an equal amount of effort from both people :D 

It takes 2 to 4 years to completely get to know someone. That's why couples who are together that long before marriage are less likely to divorce...

Some people will pretend to care just so they can get a better seat to watch your struggle. 

Every helping hand is not always there to help.Strange bUt TruE people who respect love, never get their loved one.... 

And people who think love is a time pass, get true love but they don't respect it! I never had you, therefore I couldn't lose you- so why then does my heart hurt so much :)

Your parents brought you up. Don’t ever bring them down.

Don't waste your time looking back on what you have lost. Move on, life is not meant to be traveled backwards.

Every girl is beautiful, just takes the right guy to see it.

A relationship is like standing on wet cement. The longer you stand, the more difficult is to leave. :P

For those who love me, I love u too. For those who hate me I will pray for u that u may like/love me cause each day u hate me u lost a chance to be loved by me...

Quotes Of The Day

Life is a dream for the wise, a game for the fool, a comedy for the rich, a tragedy for the poor. 

Live your everyday of life like its the last day of your life :) 

Nice Saying: You don't really need someone to complete you. You just need someone to accept you completely.. 

That is the beauty of a relation.....! If someone really loves you, no matter how many other people they meet, their feelings for you wouldn't change.

A real lover can't be stolen. I wish I had a delete button in my life to delete some memories and feelings. 

Life is too short to start your day with broken pieces of yesterday, it will definitely destroy your wonderful today and ruin your great tomorrow. 

Real relationship don't just happen. They take time and patience and two people who truly want to be together.

I'm not perfect, nobody is. But, at least I try not to hurt those people who care and care for those people who've been hurt♥ 

Accept responsibility for your life. Know that it is you who will get you where you want to go, no one else.

Never let the experiences of your past bring harm to your future. Remember, your past can’t be altered and your future just doesn't deserve the punishment.

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