« Return to the blog

Tom - Director

Posted by Tom on 7th July 2009

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.

Add A Comment

:
:
:
: Captcha

Recent Blogs

Evie-Designer

Gradient Meshes in Adobe Illustrator

There's more to Adobe Illustrator than basic shapes and blocks of colour, and there's more to gradients than linear or radial effects. Read More »

Evie-Designer

Web Design Trends 2011

Web designers are shying away from creating gimmicky tricks, but rather clean, accessible, bug-free coding - that works. Read More »

Chris-Project Manager

Let's Do Business - Eastbourne 2011

Find out what Switchplane did at Let's Do Business Eastbourne 2011. Read More »

Joel-Director

Integrating with Kashflow using PHP

Find out how to use PHP's SoapClient class to integrate with Kashflow. Includes sample code and best practice tips. Read More »

Tom-Director

Improving Search with Levenshtein Distance

Find out how we've made Honey Barrett's document management system search facility handle spelling mistakes. Read More »

Joel-Director

The New Switchplane Website

Our new website is ready - read about what we've updated and why, and get ready for Project Awesome! Read More »

Michael-Developer

Website Accessibility

Have you ever considered the importance of accessibility on your website? Read More »

Chris-Project Manager

Networking Tips

Business networking tips and trips featuring the adventures of Norm! Read More »

Michael-Developer

How Amazing Databases Are

A blog post about the usefulness of databases, using a person management system as an example. Read More »

Christian-Designer

Reasons to use Vector Graphics over Rasters

When should you use vectors or rasters, and does it even matter? Read More »

Michael-Developer

The Importance of Variables

Variables are useful - make sure you use them properly! Read More »

Chris-Project Manager

Some thoughts on writing for your business website...

Read some tips on preparing copy for your website. Read More »

Chris-Project Manager

Let's Do Business - Hastings 2010

Switchplane attended LDB Hastings in 2010 - read about our day. Read More »

Chris-Project Manager

Lets Do Business Eastbourne 2010

Switchplane's experience at Let's Do Business 2010 in Eastbourne. Read More »

Evie-Designer

Web Design Trends 2010 Part 1

A guide to web design trends 2010, including hand-drawn and painted layouts, typefaces, modern vectors and large headers and footers. Read More »

Evie-Designer

Common Printing Problems and how to avoid them

Even if it looks good on your screen, it isn't guaranteed to come out like that! I've selected a few of the most common print problems to watch out for. Read More »

Evie-Designer

Web design trends 2009 Part 2: Old and Torn Paper

Make your website a real piece of personal artwork by collecting and scanning-in torn and crumpled paper and using it for the design of your website (or just take a visit to istock photo!). Read More »

Evie-Designer

Web design trends 2009 Part 1: Badges

Subtle elements and new layout ideas for 2009. My guide on how to make your website look fashionable and unique, packed full of information and further resources. Read More »

Vanessa-Account Manager

Lets Do Business Eastbourne 2009

Last Thursday (25th June) Switchplane attended their first Lets Do Business event at the Winter Gardens in Eastbourne. Read More »

Vanessa-Account Manager

Top 10 Sales Tips

Times are tough. Sales are down. Morale is low. Sound familiar? Well it needn't be. Below are a few tips which are sure to help you re evaluate and succeed in pushing your sales margins. Read More »

Evie-Designer

Create printable artwork in Indesign and Photoshop

A few useful steps to help you successfully prepare your artwork for print, using Adobe Photoshop and Adobe Indesign. Learn about colour modes, resolution, size and other helpful tips! Read More »

Cron-Administrator

Search engine ranking more important than ever

Search engine optimisation is now an essential part of a successful website. And because of the way search engines now work, the focus of SEO is now about producing quality content and getting your name out there so you get incoming links. Read More »

Cron-Administrator

Four Months at Switchplane

I've been at Switchplane for four months now - to celebrate the launch of our new website I'm sharing some thoughts on what it's like working here. Read More »

Joel-Director

How to run a cron job on the first weekday of the month

Often it's useful to generate automatic reports or perform some other task at the start of the month. Find out how to schedule a cron job to run on the first weekday of each month. Read More »