Request quote

How to use google jQuery CDN for Elgg websites?

Posted on: December 28th, 2012 by Raez Mon No Comments

This is a simple tutorial for creating an elgg plugin which will let you use jQuery CDN for elgg websites.

Type : Tutorial | Time : Less than 5 minutes | Difficulty : None

Why should I use jQuery CDN?

Better Caching : when you’re hosting jQuery locally in your website then your visitors must download it at least once. Each of your users probably already has dozens of identical copies of jQuery in their browser’s cache, but those copies of jQuery are ignored when they visit your site. Remember these jQuery files are not small. They comes to around 250-350KB and you will loose two http requests too when these contents are loaded.

Increased parallelism : Using the Google AJAX Libraries CDN eliminates one request to your site, allowing more of your local content to downloaded in parallel. It doesn’t make a gigantic difference for users with a six concurrent connection browser, but for those still running a browser that only allows two, the difference is noticeable.

Decreased Latency : A CDN — short for Content Delivery Network — distributes the static contents of a website across servers in various, diverse physical locations. When a user’s browser resolves the URL for these files, their download will automatically target the closest available server in the network.

Minfied and gZipped : The google jQuery files are minified and served with gzip encoding, so that they will consume only less bandwidth and loads faster.

How to implement this in Elgg?

We just need to create a new plugin for this. Lets call the plugin jQueryCDN. Create a folder called jQueryCDN inside your mod directory. Now just add two files into it with the following contents

1] manifest.xml
This file will tell elgg about the plugin, version, author, licence etc..

<?xml version="1.0" encoding="UTF-8"?>
<plugin_manifest xmlns="">
	<author>Team Webgalli</author>
	<description>Load the jQuery CDN</description>

2] start.php
This file loads the jquery CDN when elgg loads

	elgg_register_event_handler('init', 'system', 'jQueryCDN_init');

	function jQueryCDN_init() {
		// Unregister Elgg's Jquery
		// Register CDN jQuery
		elgg_register_js('jquery-cdn', '', 'head');
		// Load CDN jQuery

		// Unregister Elgg's Jquery UI
		// Register CDN jQuery UI
		elgg_register_js('jquery-ui-cdn', '', 'head');	
		// Load CDN jQuery UI

Now go to the sites’s admin panel > Plugins and enable the jQueryCDN plugin.

Thats it. If you have any questions or thoughts post it as comments below.

Tags: , , ,