0
  1. I have page with many doubleclick scripts
  2. I have js code inserted at the body top (via 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 says that matches returns only 2 of ad.doubleclick.net tags. I thought first that code can not access whole body if not placed at the body very bottom. But it finds 2 tags inside div "interstitial_wrapper" which comes after my code.

So my questions are:

  • why is it so
  • How to access whole body form body start ( i may not use body 'onload' event. it is required to use script asap)

Please take a look at http://wap7.ru/folio/bannerstat/partners/doubleclick2.html and see view source, because it is too large to include here.

4

1 回答 1

2

您不必绑定到 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 );
});
于 2012-02-21T12:10:05.773 回答