Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#7404 closed bug (worksforme)

getScript does not wait until code has finished executing before callback is triggered

Reported by: darrel.grant@… Owned by:
Priority: low Milestone: 1.5
Component: ajax Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:

Description

$.getScript("mylib.js", function() {

thingFromMyLib();

});

Result: thingFromMyLib() is not a function

Affects Firefox 3.6.3 but I've been reading about the issue and other browsers are affected, inconsistently.

Change History (8)

comment:1 Changed 9 years ago by Rick Waldron

Component: unfiledajax
Priority: undecidedlow
Resolution: worksforme
Status: newclosed

Tested:

  • FF3.0.12,3.6.12,4b6 (sorry, no 3.6.3)
  • Chrome 5,6,7,8
  • Safari 5
  • IE6,7,8
  • Opera10.60

http://jsfiddle.net/rwaldron/Tqybd/1/

Last edited 9 years ago by Rick Waldron (previous) (diff)

comment:2 Changed 9 years ago by darrel.grant@…

I'm not sure how to trigger it exactly. In my case the files I'm including contain one object with many methods and more often than not, they objects are undefined when I try to use them in the callback.

Seriously, don't just close it because you couldn't reproduce it. I didn't report the bug just for fun.

I hope this is appreciated and worked on by the team, but here's the least offensive of all possible workarounds I've encountered:

var timer = setInterval( function() {

Unfortunately hacky fact: We must add another case here for each object being preloaded by one of the dependencies. if ( window.UI !== undefined ) {

clearInterval( timer ); init(); init now runs when window.UI is loaded

}

}, 100 );

comment:3 Changed 9 years ago by Rick Waldron

I'm sorry, but I didn't just close it because I couldn't reproduce it. I closed it because it's not a bug with jQuery and if it were I would've come across it while I was testing in those browsers. Additionally, I was requesting resources over network, so if something was going to be wrong, I gave it plenty of opportunity.

Lastly, you ignored the notice that says all bugs should be filed with a jsFiddle test case - I had to make up a test case for your bug.

comment:4 Changed 9 years ago by darrel.grant@…

jQuery normalizes interactions with browsers. The function of getScript() is to include libraries and right now, I have a case where it doesn't do that.

Discussion of the issue: http://stackoverflow.com/questions/1130921/is-the-callback-on-jquerys-getscript-unreliable-or-am-i-doing-something-wrong

Note the comments near the bottom of this page: http://api.jquery.com/jQuery.getScript/

The workaround I poorly wrote above, is to use setInterval() and check if the objects you are trying load are defined or not. It's not an ideal api but it has solved the problem which may or may not technically be a jquery bug.

comment:5 Changed 9 years ago by Rick Waldron

Can you please create a jsFiddle that calls in your library remotely using $.getScript()?

"jQuery normalizes interactions with browsers." You're totally right - I got the same non-broken behaviour across all the browsers I tested in.

comment:6 Changed 9 years ago by vitaliy@…

Duplicate of http://bugs.jquery.com/ticket/7404 There is a solution in there as well.

comment:7 in reply to:  6 Changed 9 years ago by anonymous

Replying to vitaliy@…:

Duplicate of http://bugs.jquery.com/ticket/7404 There is a solution in there as well.

Sorry, I meant: http://bugs.jquery.com/ticket/7130

fail.

comment:8 Changed 7 years ago by MGJP

Ran into this recently when using the Youtube API, when trying to get data from from gdata json. This is the outline of the code that did not work:

function getscriptcallback(data) {
    //callback function to output data from the video
}

function inputonchange() {
    //some code to determine and catch the video ID.
    //Then finally:
    $.getScript('http://gdata.youtube.com/feeds/api/videos/' + videoId + '?v=2&alt=json-in-script&callback=getscriptcallback');
}

input.change(function(){
    inputonchange();
});

The Above code would invariable return "getscriptcallback is not defined" (and I was using Chrome). So the way I fixed it was to make sure that the getScript only fired on document.ready by wrapping it in: $(function(){}), like so:

function getscriptcallback(data) {
    //callback function to output data from the video
}

function inputonchange() {
    //some code to determine and catch the video ID.
    //Then finally:
    $(function(){
        $.getScript('http://gdata.youtube.com/feeds/api/videos/' + videoId + '?v=2&alt=json-in-script&callback=getscriptcallback');
    });
}

input.change(function(){
    inputonchange();
});

And this seems to do the trick.

Note: See TracTickets for help on using tickets.