[SOLVED] Load a script later after app init


#1

Hey team,
I need a function like the jquery getscript() function in Dom7… I just don’t wanna include jquery as a separate script just to use one function… I have an online script that I want to use at a certain page otherwise I don’t load it… Reason is: I have realised that loading it in the tags, if the connection is slow, the app has a problem in starting up… It’s the online scripts causing that…

I tried to create append the script tags with it into the head tags in the from the specific component pageInit, It appends it, when I check the elements in the console but I can’t use the functions of the script because they are not in the Dom, or loaded (I don’t know) saying they ain’t undefined…

function LoadAlgoriaAPI() { var script = document.createElement("script"); script.src = "some-script.js"; script.type = "text/javascript"; document.getElementsByTagName("head")[0].appendChild(script); }

Any remedies? @nolimits4web, thanks a lot


#2

Hi, i use some code similar to yours. but it works fine for me.

          try {
            var po = document.createElement('script');
            po.type = 'text/javascript';
            po.async = false;
            po.src = js_url_root;
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(po, s);
            resolve('Exito app.js')
          } catch (e) {
            reject(e)
          }

In fact i load 4 files like this, 1 css and 3 js.
Can you make a jsfiddle with the error?


#3

Great, thanks @pvtallulah, kindly help me understand your code though…

 var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(po, s);
            resolve('Exito app.js')... 

is Exito a function? it looks like a string…

once again thanks…


#4

yes, it was a portion of a script, i use the string to return success when getting the script, never mind that, just return whatever you like


#5

Thanks but see, I still am getting the error because the script isnot loaded into the DOM…

Capture

I run the function on component, on pageInit… Including the script in head tags works seamlessly… prob is internet poor connection…


#6

Ok, so you are calling “places” before it has downloaded the script. right?
you can use a promise to see when the script has been loaded.


#7

Great man… Jeez, am an idiot, how could I forget that??? Even after your example?

Thanks bro… Bless’p!
@pvtallulah