Load jQuery if not already loaded

I was working on jQuery based website widget but I can not include it directly because some website already has jQuery in their pages. So I found a solution to load jQuery if not already present in current page.

Here is the solution

function loadjQuery(url, success)
	var script     = document.createElement('script');
	script.src = url;
	var head = document.getElementsByTagName('head')[0],
	done = false;
	// Attach handlers for all browsers
	script.onload = script.onreadystatechange = function() 
		if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete'))
		done = true;
		script.onload = script.onreadystatechange = null;
if (typeof jQuery == 'undefined')
    loadjQuery('http://code.jquery.com/jquery-1.10.2.min.js', function() 
			// Write your jQuery Code
	  // jQuery was already loaded
	 // Write your jQuery Code


Published by Sunny

A Mechanical engineer by education and web developer by passion and profession. He lives in Lucknow, India. Currently earning his livings by hitting keyboad. ;)

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *