7

I have a number of tracking scripts and web services installed on my website and I noticed when one of the services goes down, it still tries to call the external javascript file hosted on a different server. In Firefox, Chrome and other new browsers, there doesn't seem to be any issues when one of the services go down. However, in IE7 and IE8, my pages don't load all the way and time out before everything is displayed. Is there any way to add a time out on these javascript calls to prevent them from breaking my pages when they go down?

3 Answers 3

5

You can load them dynamically after page load with JS. If the JS files are on a different server, the browser will still show a "browser busy" indicator when you do that, but the original page will load.

If you can fetch the JS from your own site, you can load it with XMLHttpRequest after page load (or with your favorite JS library's helpers, e.g. jQuery's $.ajax(...)) and then eval it. This way the fetching itself won't show the browser-busy indicator.

To fetch the JS from your own site, you can download it from your tracking provider (which won't be officially supported but usually works) - just remember to refetch new versions every once in a while - or you can create a "forwarding" service on your own site that fetches it from the tracking provider and caches it locally for a while. This way your JS won't be in danger of staleness.

Steve Souders has more information about deferred loading of scripts and browser-busy indicators.

Sign up to request clarification or add additional context in comments.

Comments

3

Try adding defer="defer"

The defer attribute gives a hint to the browser that the script does not create any content so the browser can optionally defer interpreting the script. This can improve performance by delaying execution of scripts until after the body content is parsed and rendered.

Edit
This will prevent those scripts from running until the page loads:

function loadjs(filename) {
    var fileref=document.createElement('script');
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
}

window.onLoad = function() {
   loadJs("http://path.to.js");
   loadJs("http://path.to2.js");
   ...
}

3 Comments

Yah I dont think that works. I think the real issue is IE is trying to connect to the external JS file and the default timeout is set too high. Where as firefox and the other browsers seem to know quickly that the file isn't responding and move on accordingly. I wonder if there is any way to prevent that....
Well you could load external javascript files using javascript, not html. Put the loading code in a <script defer=defer> block, or do the loading in an onLoad handler. @orip he is only having problems with IE. so it doesn't matter if firefox/chrome doesn't support this tag.
fwiw, defer and adding the script after onload are different. Onload fires after all assets (including non-critical ones like images) have loaded. defer just defers script execution until after the DOM becomes "interactive," i.e. the DOM is parsed. This still happens before the "onload" event.
3

If you need to load external scripts and you want to enforce a timeout limit, to avoid having a busy indicator running for too long, you can use setTimeout() with window.stop() and, the IE equivalent:

http://forums.devshed.com/html-programming-1/does-window-stop-work-in-ie-1311.html

    var abort_load = function() {
      if(navigator.appName == "Microsoft Internet Explorer") {
          window.document.execCommand('Stop');
      } else {
          window.stop();
      }
    };
    /**
     * Ensure browser gives up trying to load JS after 3 seconds.
     */
    setTimeout(abort_load, 3000);

Note that window.stop() is the equivalent of the user clicking the stop button on their browser. So typically you'd only want to call setTimeout() after page load, to ensure you don't interrupt the browser while it's still downloading images, css and so on.

This should be combined with the suggestions made by orip, namely to load the scripts dynamically, in order to avoid the worst case of a server that never responds, resulting in a "browser busy" indicator that's active until the browser's timeout (which is often over a minute). With window.stop() in a timer, you effectively specify how long the browser can try to load the script.

Also note that setTimeout()'s interval is not that precisely interpreted by browsers so round up in terms of how much time you want to allow to load a script.

Also, one counter-indication to using window.stop() is if your page does things like scroll to a certain position via js. You might be willing to live with that but in any case you can make the stop() conditional on NOT having already loaded the content you expected. For example if your external JS will define a variable foo, you could do:

var abort_load = function() {
  if (typeof(foo) == "undefined") {
    if(navigator.appName == "Microsoft Internet Explorer") {
        window.document.execCommand('Stop');
    } else {
        window.stop();
    }
  }
};

This way, in the happy path case (scripts do load within timeout interval), you don't actually invoke window.stop().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.