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;
	head.appendChild(script);
	// Attach handlers for all browsers
	script.onload = script.onreadystatechange = function() 
	{
		if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete'))
		{
		done = true;
		success();
		script.onload = script.onreadystatechange = null;
		head.removeChild(script);		
		}
	};
		
	
}
if (typeof jQuery == 'undefined')
{
	
    loadjQuery('http://code.jquery.com/jquery-1.10.2.min.js', function() 
          {
			// Write your jQuery Code
           });
	
} 
else 
{ 
	  // jQuery was already loaded
	 // Write your jQuery Code

}

Liked It? Get Free updates in your Email

Delivered by feedburner

One thought on “Load jQuery if not already loaded

  1. Nikolay
    #

    It’s work. The only working scripts. You save my day. Very thanks.

    PS: Sorry for my English.

    Reply

Leave a Reply

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