0

I know this has been asked before but what I need help with is not loading the JS, I already do that but I need to make it asynchronously.

I'm making and "embed code" and I need some libraries loaded in the website for what it's embedded to work. This includes jQuery and a few plugins all minified and unified into a separated file but inserting a script tag by document.write or document.getElementsByTagName('head')[0].appendChild(element); doesn't make it asynchronous so the first $('#selector').something() right after the document.write fails because jQuery isn't loaded.

If I place the whole minified code inside the main file instead of loading it in a second call everything works perfectly, but I don't want to include it there because the code is embedded twice or more so I'd be loading the libraries twice. What I'm doing is to detect if there is a previous instance of the embedded code: if it isnt't then load the libraries, if it's a second instance I don't load it. The problem is that as the browser is not waiting for the libraries to be loaded before continuing, the code fails.

Any ideas? Thanks!

Juan Ignacio
  • 3,217
  • 8
  • 33
  • 53
  • one more thing, I cannot rely on the async attribute since because we expect old browsers using this website – Juan Ignacio Aug 20 '13 at 05:40
  • Does this answer your question (fully read the accepted answer especially the code taken from jQuery, I think it's what you're looking for): http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer – slebetman Aug 20 '13 at 06:20
  • I really think you should not embed the code more than once (most libraries handle this situation correctly, but I have met a couple that can bug out on that). Maybe you should look in that direction to solve your problem? – pckill Aug 20 '13 at 08:00

0 Answers0