While it seems like lots of people seem to hav been having trouble with this on Stack Overflow, I've yet see a description of why it was going wrong (1, 2, 3)
My code (for testing purposes):
jQuery.getScript("res/fader.js", function () { alert("loaded"); });
works flawlessly in Chrome (16). In Firefox (11) the firebug console shows the script being requested and the expected response (including the mimetype of application/javascript). But no alert. No error. Nothing.
If I subsequently try to reference something in the script which should have been loaded, it is still undefined (this is several seconds after the onload event and after the console shows the script has been retrieved).
update
I'm using jQuery 1.9.1
jQuery getScript() Method The getScript() method is used to get and execute a JavaScript using an AJAX HTTP GET request.
not defined: In JavaScript, it is one of the reference errors that JavaScript will throw when someone accesses the variable which is not inside the memory heap.
33 As of jQuery 1.5 you can append a .fail to your call to getScript. $.getScript('foo.js', function(){ //script loaded and parsed }).fail(function(){ if(arguments[0].readyState==0){ //script failed to load }else{ //script loaded but failed to parse alert(arguments[2].toString()); } })
Prior to jQuery 3.5.0, unsuccessful HTTP responses with a script Content-Type were still executed. By default, $.getScript () sets the cache setting to false. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested.
$.getScript("script_test.js", function () { if (typeof script_test !== undefined) alert("script has been loaded!"); Or you could just try and see if whatever is in your script, actually exists--functions, variables, objects, etc.
Included scripts can have some impact on the current page. The callback is fired once the script has been loaded but not necessarily executed. Scripts are included and run by referencing the file name: console.log ( "Load was performed." ); As of jQuery 1.5, you may use .fail () to account for errors:
Kudos and thanks to uncollected (add an answer & I'll mark it as accepted) adding the exception hander, I found the problem...
An exception was occurring - which was being handled silently by jQuery. Adding the handler via getScript() revealed the problem was "invalid assignment left-hand side". Unfortunately this exception doesn't say where the error occurred. I then tried a different method of loading the script (creating a script element and appending it to the body) which meant that the default handler in the browser was triggered which gave me the line number and the bit of offending code:
this=null;
This is intended to clean up the object instance, to avoid a memory leak and works in Chrome, but not in Firefox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With