Lev Savranskiy
Lev Savranskiy

Reputation: 430

getElementsByTagName issue

  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:

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.

Upvotes: 0

Views: 233

Answers (1)

Rob W
Rob W

Reputation: 349262

You don't have to bind to the onload event. Just bind to the DOMContentLoaded event.

Since you've already included jQuery in your page, this can esily be done using .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 );
});

Upvotes: 2

Related Questions