When adding a script loaded dynamically, I can access main object during execution but once the page is loaded, if I try to access that from console, it throws undefined. For 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 = ()=> { /* do something when script loads */}
fjs.parentNode.insertBefore(js, fjs);
This should ideally append FB object to window, and it does that during execution of page. I can access window.FB while the aforesaid JS is executing, but once the page is loaded (and I know that js.onload is also executed), I can't access FB.someOperation.
What is the solution to ensure that dynamically added script is always available (even after page has loaded)
onLoadshould beonload