jQuery Interview Questions and Answers (185) - Page 2

What if the latest jQuery version is available and I am still referring older version of jQuery file from CDNs?

Do not worry about it, it?s a general promise made by CDNs that they will remain hosting the older version of the files on the same location where they had initially released; so even if newer version of the files are released, the older version remains there on the CDNs and your web page still works.
How to load local jQuery file in case CDN is not available?

Sometimes, it may happen that the CDN you have used to load the jQuery file is not available (it rarely happens, however anything is possible, isn?t it?); in that case you should load your local jQuery file that is available on your server so that all jQuery related functionality still work on your page.

Write following lines of code

<!-- START - jQuery Reference -->
<script type="text/javascript" language="Javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.min.js "></script>

<script type='text/javascript'>//<![CDATA[
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/Script/jquery-1.4.1.min.js' type='text/javascript' %3E%3C/script%3E"));
}//]]>
</script>

<!-- END - jQuery Reference -->

Replace bolded path with your own jQuery file path on the server. In the above code, first line tries to load the jQuery file from CDN, if browser could load the file successfully, “jQuery” variable will not be undefined and next script will not run otherwise next script will run that will write the script tag to load the jQuery file from your server.
How to execute jQuery code?

1. As and when page loads, execute the jQuery code

 <script language="javascript" type="text/javascript">

$(function () {
$("#div1").css("border", "2px solid green");
});
</script>

OR

   <script language="javascript" type="text/javascript">

$("#div1").css("border", "2px solid green");
</script>


The benefit of executing jQuery code in this way is that it doesn?t wait the whole page to load completely, so in case you want user to see the effects as soon as the corresponding elements are loaded, you can use this.

However the disadvantage is that if the element on which jQuery has to execute has not loaded then it will error out or you will not get desired result; so while using this way of executing jQuery code, you will have to make sure that the element on which you want to work with jQuery is loaded first (you can place your jQuery code right after your HTML element).

2. Execute jQuery only when the complete DOM objects (the complete page has been loaded). You will have to wrap your code in .ready function.

<script language="javascript" type="text/javascript">

$(document).ready(function () {
$("#div1").css("border", "2px solid green");
});
</script>


This is the better and safer way to execute jQuery. This makes sure that jQuery code will execute only if complete page has been loaded in the browser so you are rest assured that user will not see any undesired behavior on the page.

As a developer, the decision of where and how to write jQuery code lies on you. I prefer to use 2nd method as it ensures that my complete page is loaded in the browser and I am ready to play with any element available on the page.
jQuery Core

jQuery accepts a string enclosed with double quote (“”) that can contain a CSS selector which is used to match a set of elements on the web page.

jQuery code can start with either “jQuery” word or a “$” (dollar symbol). Take a look at below code snippet

<script language="javascript" type="text/javascript">

$(function () {
jQuery("#div1").css("border", "2px solid red");
});
</script>


OR

<script language="javascript" type="text/javascript">

$(function () {
$("#div1").css("border", "2px solid green");
});
</script>


Both above code snippets are functionally same and do the same thing. So you can either user jQuery or $ when you are writing jQuery code.
How to give alert message in jQuery on a Button Click ?

Hi all,
First, include jQuery in your application.

Drop a textbox in your .aspx page:-
<input id="inputField" type="text" size="12"/> 


include a button also:-
<asp:Button ID="Button1" runat="server" Text="get"/> 


Now, here's the script:-


<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function () {
alert($('#inputField').attr("value"));
});
});
</script>



On the click of the button, an alert will be given containing the text in the text box.

I hope this helps......
Thanks and Regards
Akiii
What are Selectors in jQuery mean ?

Generally in HTML, if we need to work with any control on a web page we need to find the control. For that we use document.getElementByID or document.getElementByName. But in jquery we do it using Selectors.

Using this selectors we can select all the controls as well using a symbol (* )

A sample code snippet can be of this form

<script language="javascript" type="text/javascript">

$("*").css("border", "10px red");
</script>


This will make all the borders in the web page with a width of 10 pixel and color as red.
Is it good to load jquery from CDN(Content delivery network) ?

Yes, it is always good to load your jquery from content delivery network. It provides some benefits like :-
(1) Caching - It means that if any previously visited site by user is using jQuery from Google CDN then the cached version will be used. It will not be downloaded again.
(2) Reduces load - It reduces the load on your web server as it downloads from Google server's.

Example :-

<script  type="text/javascript"

src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>


Thanks and Regards
Akiii
Do we need to add the JQuery file both at the Master page and Content page as well?

No, if the Jquery file has been added to the master page then we can access the content page directly without adding any reference to it.

This can be done using this simple example

<script type="text/javascript" src="jQuery-1.4.1-min.js"></script>

What is the advantage of using the minified version of JQuery rather than using the conventional one?

The advantage of using a minified version of JQuery file is Efficiency of the web page increases.

The normal version jQuery-x.x.x.js has a file size of 178KB

but the minified version jQuery.x.x.x-min.js has 76.7 KB.

The reduction in size makes the page to load more faster than you use a conventional jQuery file with 178KB
What is CDN and how jQuery is related to it?

CDN - It stands for Content Distribution Network or Content Delivery Network.

Generally, a group of systems at various places connected to transfer data files between them to increase its bandwidth while accessing data. The typical architecture is designed in such a way that a client access a file copy from its nearest client rather than accessing it from a centralized server.

So we can load this jQuery file from that CDN so that the efficiency of all the clients working under that network will be increased.

Example :

We can load jQuery from Google libraries API

<script type="text/javascript" language="Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

Can we select a element having a specific class in jQuery ?

Yes, we can select an element with a specific class, we use the class selector.The class name must contain the prefix as "." (dot).

<script language="javascript" type="text/javascript">


$(".class1").css("border", "2px solid red");

</script>


Above code will select all the elements of the webpage containing the class as "class1" and apply the css style border width as 2 Pixel, style as solid and color as red.
What is the use of Delegate() Method in jQuery?

The delegate() method can be used in two ways.
1) If you have a parent element, and you want to attach an event to each one of its child elements, this delegate() method is used.
Ex:Un-ordered List
Instead of attaching an event to each <li> element, you can attach a single event to <ul> element.

Example:

$("ul").delegate("li", "click", function(){

$(this).hide();
});


2) When an element is not available on the current page, this method is used.
.live() method is also used for the same purpose but, delegate() method is a bit faster.

Example:

$("ul").delegate("li", "click", function(){

$(this).hide();
});


This will hide any list items that are not currently available on the page. They may be loaded via an Ajax request and then append to it.
Using .bind() or .click() methods, you would have to manually attach events to these new list items once they are added.
Which sign does jQuery use as a shortcut for jQuery?

NOTE: This is objective type question, Please click question title for correct answer.
JQuery is a client scripting.

NOTE: This is objective type question, Please click question title for correct answer.
Can be used JQuery with AJAX?

NOTE: This is objective type question, Please click question title for correct answer.
JQuery is JavaScript Library or JSON Library?

JQuery is a javascript library not JSON Library. Jquery library is single JavaScript file that contains Common DOM, event effects and Ajax functions.

"JQuery is a library of javascript function."

For more Info: http://jquery.com/
What does .size() method of jquery return ?

.size() method of jquery returns number of element in the object. That means that you can count the number of elements within an object.

For example :-

$(document).ready(function(){ 

var Count = $("div").size();
alert(Count);
});



Thanks and Regards
Akiii
What is the difference between jquery.size() and jquery.length ?

jquery.size() and jquery.length both returns the number of element found in the object. But, jquery.length is faster than jquery.size() because size() is a method but length is a property .

So, there is always an overhead in calling a function.


Thanks and Regards
Akiii
How to set Page Title using jQuery ?

$(function(){

$(document).attr("title", "Dotnet Funda");
});




Thanks and Regards
Akiii
How will you Encode/Decode URL in jQuery ?

In jquery, you can use the following functions to encode and decode url :-

encodeURIComponent(url) and decodeURIComponent(url)


You have to pass the complete url with parameterized value in the function and in return it will encode/decode you url for you !



Thanks and Regards
Akiii
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories