Category Archives: jQuery

jQuery $(this) And ‘this’

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.

$(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.

$(document).ready(function(){
$(‘#spnValue’).mouseover(function(){
alert(this.innerText);
});
});

jQuery Selectors

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:

  • Name: Selects all elements which match with the given element Name.
  • #ID: Selects a single element which matches with the given ID
  • .Class: Selects all elements which match with the given Class.
  • Universal (*): Selects all elements available in a DOM.
  • Attribute Selector: Select elements based on its attribute value.

Load jQuery Locally When CDN Fails

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 the jQuery.js file from your folder.

<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.

Load jQuery From CDN

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>

jQuery CDN

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

jQuery Different Version

jQuery library comes in 2 different versions.

  • Development
  • Production/Deployment

The development 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 development 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.