Tag Archives: Javascript

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.

jQuery.noConflict

As other client side libraries like MooTools, Prototype can be used with jQuery and they also use $() as their global function and to define variables. This situation creates conflict as $() is used by jQuery and other library as their global function. To overcome from such situations, jQuery has introduced jQuery.noConflict().

JavaScript Errors

There are three types of errors:

  • Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load time errors and it generates the errors dynamically.
  • Run time errors: Errors that come due to misuse of the command inside the HTML language.
  • Logical Errors: These are the errors that occur due to the bad logic performed on a function which is having different operation.