Side navigation
#7404 closed bug (worksforme)
Opened November 04, 2010 10:13PM UTC
Closed November 04, 2010 10:21PM UTC
Last modified August 14, 2012 09:21AM UTC
getScript does not wait until code has finished executing before callback is triggered
Reported by: | darrel.grant@neverblue.com | 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.
Attachments (0)
Change History (8)
Changed November 04, 2010 10:21PM UTC by comment:1
_comment0: | Tested: <br> \ FF3.0.12,3.6.12,4b6 (sorry, no 3.6.3)<br> \ Chrome 5,6,7,8<br> \ Safari 5<br> \ IE6,7,8<br> \ Opera10.60<br> \ \ \ http://jsfiddle.net/rwaldron/Tqybd/1/ → 1288909345926134 |
---|---|
component: | unfiled → ajax |
priority: | undecided → low |
resolution: | → worksforme |
status: | new → closed |
Changed November 04, 2010 10:32PM UTC by comment:2
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 );
Changed November 04, 2010 10:42PM UTC by comment:3
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.
Changed November 04, 2010 10:50PM UTC by comment:4
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:
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.
Changed November 04, 2010 10:55PM UTC by comment:5
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.
Changed March 31, 2011 06:32PM UTC by comment:6
Duplicate of http://bugs.jquery.com/ticket/7404
There is a solution in there as well.
Changed March 31, 2011 06:32PM UTC by comment:7
Replying to [comment:6 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.
Changed August 14, 2012 09:21AM UTC by comment:8
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.
Tested:
http://jsfiddle.net/rwaldron/Tqybd/1/