Timeline for Is this a good approach to loading JavaScript files asynchronously?
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 22, 2014 at 23:58 | vote | accept | Nate | ||
Jul 20, 2014 at 21:16 | history | tweeted | twitter.com/#!/StackCodeReview/status/490968494058790913 | ||
Jul 20, 2014 at 8:04 | comment | added | Dagg |
Ah, I see it now @Nate, you're passing in the name of a global function as a string. When I skimmed this question earlier I was looking for an actual function being passed in. Not sure why you'd do this instead of passing an actual function, seems less versatile and pollutes global scope. Anyway, I see no reason to prefer this over an AMD loader (and I see reasons not to prefer it). BTW if you want to support IE<=8 (I gather you do since you have a branch for attachEvent here) you'll need to use the readystatechange event instead of load .
|
|
Jul 20, 2014 at 7:57 | answer | added | maja | timeline score: 5 | |
Jul 19, 2014 at 23:05 | comment | added | megawac |
What do you mean $.getScript(url).then(function() {console.log('im here')}) . Similar ideas for those amd loaders
|
|
Jul 19, 2014 at 23:01 | comment | added | Nate | @Dagg "One problem with your approach is there's no way to know when things are loaded." If you look at my code, you'll see it support an onload callback, so I can call a function or set a flag or whatever with it. | |
Jul 19, 2014 at 22:47 | comment | added | Dagg | @Nate I'd suggest using an AMD loader. Nice standardized way to do async loading during development, and later on you can set up a build process to merge all the scripts together. One problem with your approach is there's no way to know when things are loaded. | |
Jul 19, 2014 at 22:26 | comment | added | Thomas Junk | @Nate Not really. One trick I could think of is, that you embed an IIFE in the script, which is executed after the file is loaded and parsed. | |
Jul 19, 2014 at 20:01 | comment | added | Nate | @megawac Interesting idea on using jQuery. I did a little searching and apparently getScript() uses eval, which I've read is bad. I also found an article which says the callback is fired onload not when the script has been parsed and is available to use, but I don't know if that's correct or not (looseideas.co.uk/getscript-callbacks-defer-async). | |
Jul 19, 2014 at 20:00 | comment | added | Nate |
@ThomasJunk very interesting.. That's contrary to pretty much everything I've read on the subject, but the author does provide evidence to support his position. The question is, if I use the async attribute, can I specify a callback somehow?
|
|
Jul 19, 2014 at 18:36 | comment | added | megawac |
Also my check would probably be entry[1] != "" && typeof window[entry[1]] != "undefined"
|
|
Jul 19, 2014 at 18:33 | comment | added | megawac |
I'm really not a fan of your global variable onTypeadheadLoaded . There are many more elegant ways to do this. typeahead requires jQuery yes? why not use getScript
|
|
Jul 19, 2014 at 17:09 | comment | added | Thomas Junk | igvita.com/2014/05/20/… perhaps you consider using async | |
Jul 19, 2014 at 16:34 | history | asked | Nate | CC BY-SA 3.0 |