IE9 - SCRIPT5009: "jQuery" - undefined

The error code SCRIPT5009 includes the following code: "jQuery" is undefined in IE9 (possibly also in older versions of IE):

var $tx; if (window.jQuery) { $tx = jQuery; if( jQuery().jquery.replace(".", "") < 17.1 ) { addjQuery(); } } else { addjQuery(); } function addjQuery() { document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"><\/script>'); document.write('<script type="text/javascript">$tx = jQuery.noConflict();<\/script>'); } document.write('<script src="workingScript.js"><\/script>'); 

I solved it! It works fine like this:

 var $tx; if (window.jQuery) { $tx = jQuery; if( jQuery().jquery.replace(".", "") < 17.1 ) { addjQuery(); } } else { addjQuery(); } function addjQuery() { loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", function(){ $tx = jQuery.noConflict(true); }); } document.write('<script src="workingScript.js"><\/script>'); function loadScript(url, callback){ var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState){ //IE script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function(){ callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); } 

workingScript.js:

 (function ($) { // some code here })($tx); 

The error here is: $ tx = jQuery.noConflict (); "if the addjQuery function is addjQuery . If the website is already using the current version of jQuery, erverythings works fine.

Does anyone have an idea how to solve this?

+4
source share
1 answer

Your script loads other scripts during its execution, they will not be executed synchronously, but rather asynchronously. When $tx = jQuery.noConflict(); running, jQuery loading is not guaranteed.

If you need to do this synchronously, see this question , or better yet, use something like RequireJS that handles this for you. (It also allows you to use this reserve).

+3
source

All Articles