Skip to content

Instantly share code, notes, and snippets.

@dialtone
Created March 21, 2012 21:21
Show Gist options
  • Save dialtone/2152947 to your computer and use it in GitHub Desktop.
Save dialtone/2152947 to your computer and use it in GitHub Desktop.
(function () {
var _onload = function(){
if (document.readyState && !/loaded|complete/.test(document.readyState)){setTimeout(_onload, 10);return}
if (!window.__adroll_loaded){__adroll_loaded=true;setTimeout(_onload, 50);return}
var scr = document.createElement("script");
var host = (("https:" == document.location.protocol) ? "https://s.adroll.com" : "http://a.adroll.com");
scr.setAttribute('async', 'true');
scr.type = "text/javascript";
scr.src = host + "/j/roundtrip.js";
((document.getElementsByTagName('head') || [null])[0] ||
document.getElementsByTagName('script')[0].parentNode).appendChild(scr);};
if (window.addEventListener) {window.addEventListener('load', _onload, false);}
else {window.attachEvent('onload', _onload)}}());
@claudioc
Copy link

You don't need line 3. For IE you can listen to script.onreadystatechange (and the subject should be the script, not the document).
Something along these lines (we use it for dinamically loading jQuery):

if (script.readyState) { // Internet Explorer
  script.onreadystatechange = function(){
    if (script.readyState === "loaded" || script.readyState === "complete") {
    script.onreadystatechange = null;
    // script loaded
    }
  };
  } 
 else { // non IE
  script.onload = function() {
    // Script loaded
  };
}

@dialtone
Copy link
Author

Line 3 is for WebKit, it happens to work also on IE. I don't need to know when the script is loaded, it's more when the page has finished loading so I can do my stuff from the script without having the user think that the page hasn't loaded yet.

@claudioc
Copy link

I don't think it matters but the IIFE (like yours) are written like (function(){})(); and not (function(){}());
HTH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment