The Joys of JQuery
JQuery is a light-weight, cross-browser and feature packed JavaScript library. To explain why JQuery is so good, first you need to know why not using it is so bad.
What is JavaScript?
JavaScript is a scripting language that programmers use to make their web pages more interactive. For example, if you have used Hotmail, Yahoo Mail or GMail, then you have experienced the power of JavaScript. When you click on any of your emails in the message list pane, then the message appears in the reading pane (without the page reloading everything), JavaScript has been used.
JavaScript was born in 1995 and was originally developed by Netscape, later in 1998 it was standardised under the name ECMAScript. Since then many scripting languages have derived from ECMAScript, most notably ActionScript which is used in the development of Adobe Flash applications and JScript, Microsoft's version of JavaScript.
So what is wrong with it?
Over the years, web browsers have implemented (and built upon) the standardised ECMAScript. Unfortunately, not all web browsers support the same functions, which leads to why not using a library like JQuery can be a major headache.
For example, here is a snippet of code that allows a JavaScript programmer to create a new XML HTTP request:
function ajaxFunction()
{
var request = false;
try
{
// Opera 8.0+, Firefox, Safari
request = new XMLHttpRequest();
}
catch(e)
{
// Internet Explorer Browsers
try
{
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
}
}
}
return request;
}
Between each of the "try" and "catch" statements is some browser-specific code. The first section ("XMLHttpRequest") is something that all modern browsers support. If this code did not have the "try" and "catch" statements, earlier versions of Internet Explorer would stop processing any more JavaScript, possibly causing the web page to stop functioning.
Luckily though, the "try" and "catch" statements let us "try" something and "catch" any errors. So, if there is an error while trying the code for modern browsers, we can try to use code specific to Internet Explorer instead! As you can see from the code, there are two further "try" and "catch" sections - these support different versions of Internet Explorer.
As you can probably imagine, having to write three different versions of the same code to support the different browsers is a nightmare - and this is just one example! So, what's so good about JQuery?
$.ajax({ type: "GET", url: "some.php", data: { request: 'loadSomething' }, success: onSuccess, error: onError });
Can you spot the difference? That's it. Job done. Not only does this one line of code create the XMLHttpRequest, it also sends a request to the place I want, in the format I want and lets me handle any errors easily! JQuery does this by doing exactly as shown earlier in my post - but that's the point, it does it all for you! JQuery takes care of all of the time-consuming, browser-dependent problems for you, making the development of rich interactive applications much, much faster.


