2

Our Tag Manager went down today causing our site to crawl to a hault. As the code in the tag manager isn't mission critical (analytics, etc...), is there a way to do something like the following?

var extScript = document.createElement('script');
extScript.type = 'text/javascript';
extScript.src = 'http://third-party.com/scriptfile.js';
var s = document.getElementsByTagName('script')[0]; 
s.parentNode.insertBefore(extScript, s);

window.setTimeout(function () {
    // if script not loaded - "give up"
}, 3000); // 3 secs
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
TomFuertes
  • 7,150
  • 5
  • 35
  • 49
  • Can you just insert the script after a few seconds to give everything else time to load first? – Joshua Dwire Nov 01 '12 at 18:43
  • Yes, of course you can set a timeout and then "give up". But what do you want to do on giving up? – Bergi Nov 01 '12 at 18:46
  • 1
    I think he was looking for a way to abort the script so the browser stops "spinning." Consequently, I'm also looking for a way to do this but haven't come up with much. – JavaKungFu Dec 10 '12 at 20:16

2 Answers2

1

If its not mission-critical, you might want to put this script at the end of the body tag, as soon as the document has fully loaded.

window.onload = function() {
  var extScript = document.createElement('script');
  extScript.type = 'text/javascript';
  extScript.src = 'http://third-party.com/scriptfile.js';

  document.body.appendChild(extScript);

}
Rob
  • 19
  • 1
0

How about adding async attribute?

<script async type = 'text/javascript' src = 'http://third-party.com/scriptfile.js' ></script>

this will not block, however you might run into some issues (like this one)

Community
  • 1
  • 1
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265