javascript - Dynamically Loaded Script is undefined after load -


when adding script loaded dynamically, can access main object during execution once page loaded, if try access console, throws undefined. example,

 var js, fjs = d.getelementsbytagname('script')[0];      if (d.getelementbyid(id)) {return;}      js = d.createelement('script'); js.id = id;      js.src = "//connect.facebook.net/en_us/all.js";  js.onload = ()=> { /* when script loads */}  fjs.parentnode.insertbefore(js, fjs); 

this should ideally append fb object window, , during execution of page. can access window.fb while aforesaid js executing, once page loaded (and know js.onload executed), can't access fb.someoperation.

what solution ensure dynamically added script available (even after page has loaded)

remember double check property names.

onload should onload (all lowercase).

var d = document;  var id = 'hello';  var js, fjs = d.getelementsbytagname('script')[0];  js = d.createelement('script');  js.id = id;  js.src = "//connect.facebook.net/en_us/all.js";    js.onload = () => {  //   ^--------------------------- note lowercase 'l'    console.log('hello world');  };    fjs.parentnode.insertbefore(js, fjs);
<script></script>


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -