我的页面中包含许多doubleclick脚本
我在主体顶部插入了js代码(通过s.src =('parts_async_dev.js')):

var innerHTML = document.getElementsByTagName('html')[0].innerHTML.toString();
var regexp = /ad.doubleclick.net/gm;
var matches = innerHTML.match(regexp);
alert('found ' + matches.length + ' tags by regexp ' + regexp);
console.log( innerHTML);



alert表示匹配项仅返回2个ad.doubleclick.net标记。
我首先想到的是,如果代码没有放在主体的最底部,则无法访问整个主体。
但是它在div“ interstitial_wrapper”内找到了2个标记,该标记位于我的代码之后。

所以我的问题是:


为什么会这样
如何访问整个正文形式的正文开始(我可能不使用正文“ onload”事件。需要使用asap脚本)


请看看http://wap7.ru/folio/bannerstat/partners/doubleclick2.html
并查看视图源,因为它太大了,无法在此处包含。

最佳答案

您不必绑定到onload事件。只需绑定到DOMContentLoaded事件。

由于您已经在页面中包含了jQuery,因此可以使用.ready轻松完成此操作:

$(document).ready(function() {
    var innerHTML = document.body.innerHTML;
    /* If you want to use a RegExp, use the following:
    var regexp = /ad\.doubleclick\.net/gi; // Note: escaped dot
    var matches = innerHTML.match(regexp);
    matches = matches ? matches.length : 0; // matches can be `null`
    */

    // This is more effective:
    var matches = innerHTML.split('ad.doubleclick.net').length - 1;
    alert('Found ' + matches + ' tags.');
    console.log( innerHTML );
});

07-24 18:24