Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery getScript issue

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

like image 849
symcbean Avatar asked Jun 05 '13 23:06

symcbean


People also ask

What is JQuery getScript?

jQuery getScript() Method The getScript() method is used to get and execute a JavaScript using an AJAX HTTP GET request.

Is not defined in JavaScript?

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.

How to add a fail statement to getscript in jQuery?

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()); } })

What is the purpose of getscript ()?

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.

How to check if a JavaScript script has been loaded?

$.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.

How do included scripts affect the current page?

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:


1 Answers

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.

like image 159
symcbean Avatar answered Oct 14 '22 01:10

symcbean