jQuery function of the day
Website Devlopment

jQuery function of the day

Website Devlopment

jQuery function of the day

Do you wish there was an easy way to have the main content of your website load asynchronously yet allow your site to still work with javascript disabled? Well, then you need to make an acquaintance with the jQuery function load.

Here’s one way one might go about using it as well.

$('a').click(function(e) {
	$('#content').load($(this).attr('href')+' #content');
	e.preventDefault();
});

And that’s basically it. The above function replaces the current content div with the content div of the link in question. What the above jQuery does is hijack the normal anchor function and replace it with a load. You will need to have e.preventDefault() in there to prevent the link from functioning normally. All that really remains is a filter to tell this code to only work for links that point to your site, not for external links. We’ll leave that as an exercise for the reader.

Write A Comment