Redirect [removed] from javascript script

前端 未结 4 1957
渐次进展
渐次进展 2021-02-11 05:37

We want to serve ads on our site but the adserver we are in talks with has issues with delivering their advertising fast enough for us.

The issue as I see it is that we

相关标签:
4条回答
  • 2021-02-11 05:55

    If I understand correctly, you want to capture document.write to a variable instead of writing it to the document. You can actually do this:

    var advertHtml = '';
    var oldWrite = document.write;
    
    document.write = function(str)
    {
        advertHtml += str;
    }
    
    // Ad code here
    
    // Put back the old function
    document.write = oldWrite;
    
    // Later...
    ...innerHTML = advertHtml;
    

    You still have the hit of loading the script file though.

    0 讨论(0)
  • 2021-02-11 06:00

    You'd run into some security issues going cross domain due to the Same Origin Policy. I would look into JSONP if you have access to change the advertising content/service

    http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback

    0 讨论(0)
  • 2021-02-11 06:01

    To decouple the main page loading from the ad loading, you can put the ad in its own page in an iframe or, similarly, download the script file with AJAX and execute it whenever it comes down. If the former is not adequate, because of referring URI or whatever, the latter gives you some flexibility: you could use string replacement to rewrite "document.write" to something else, or perhaps temporarily replace it like "document.write = custom_function;".

    0 讨论(0)
  • 2021-02-11 06:09

    You may be interesed in the Javascript library I developed which allows to load 3rd party scripts using document.write after window.onload. Internally, the library overrides document.write, appending DOM elements dynamically, running any included scripts which may use document.write as well.

    I have set up a demo, in which I load 3 Google Ads, an Amazon widget as well as Google Analytics dynamically.

    0 讨论(0)
提交回复
热议问题