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 }
It’s work. The only working scripts. You save my day. Very thanks.
PS: Sorry for my English.