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!