简体   繁体   中英

Overwriting of the document.write to delay a script's execution

I'm try for delaying the execution of this ad script:

  <script type="text/javascript">
  var _pop = _pop || [];
  _pop.push(['siteId', 809347]);
  _pop.push(['minBid', 0.000000]);
  _pop.push(['popundersPerIP', 0]);
  _pop.push(['delayBetween', 0]);
  _pop.push(['default', false]);
  _pop.push(['defaultPerDay', 0]);
  _pop.push(['topmostLayer', false]);
  (function() {
    var pa = document.createElement('script'); pa.type = 'text/javascript'; pa.async = true;
    var s = document.getElementsByTagName('script')[0]; 
    pa.src = '//URL/pop.js';
    pa.onerror = function() {
      var sa = document.createElement('script'); sa.type = 'text/javascript'; sa.async = true;
      sa.src = '//URL/pop.js';
      s.parentNode.insertBefore(sa, s);
    };
    s.parentNode.insertBefore(pa, s);
  })();
</script>

For do this I have apply setTimeout in this way:

    setTimeout (function() {
   (function() {
    var pa = document.createElement('script'); pa.type = 'text/javascript'; pa.async = true;
    var s = document.getElementsByTagName('script')[0];
    pa.src = '//c1.popads.net/pop.js';
        pa.onerror = function() {
      var sa = document.createElement('script'); sa.type = 'text/javascript'; sa.async = true;
      sa.src = '//c2.popads.net/pop.js';
    document.head.appendChild(sa, s);
    };
    document.head.appendChild(pa, s);
  })();  }, 2300);
  </script>

And changed s.parentNode.insertBefore with document.head.appendChild

The script start but I not see delay.

I have read "If the target script expects to be run synchronously and uses document.write you're out of luck. Unless, you want to do some messy hacks involving overwriting of the native document.write function.

I need for overwriting document.write ?

You can delay the execution of any script by many methods. Simple ones:

  1. Place your script tags just before the end of your page body, so that they are loaded after the browser has parsed the rest of the page. Therefore the browser is able to render the page before your script executes.
  2. Add the defer="defer" attribute to your script tag (if it loads an external file). Execution is delayed at the end of page parsing.
  3. Use a setTimeout wrapped around the code to be delayed, as you tried. Make sure the setTimeout itself is wrapped to be executed on DOM ready event , otherwise it will start the counter at execution, before the rest of the page is rendered, and you may see no effect if that rendering is slow.
  4. Load a script asynchronously after a given timeout (but refer to point 3 about DOM ready event). In your case, why not putting the ad loader code in an external file (so you do not even have to modify it!), and load it later (using a similar script loading method)? Yes, that would be loading a loader…

Side notes:

  • Use of someScriptElement.parentNode.insertBefore is considered a better practice than document.head.appendChild , as a document may not have a head tag.
  • The sentence you quoted means that any script that relies on document.write forces you to load it synchronously (and therefore document.write should be avoided whenever possible). You may try to emulate the synchronous load by overwriting the document.write function, so that you could nevertheless load the script asynchronously. But in your case, the ad code does not use document.write , and it loads another script asynchronously , so you do not have to bother.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM