SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOMContentLoaded and Script Ads

    Hello. Some sites use script elements in body area to retrieve ads e.g. adsense. But Firefox willnt fire DOMContentLoaded event until these ads retrieved completely. So sometimes DOMContentLoaded gets fire 10 seconds later it is supposed to be fired, beacuse of fetching ads slowly. Is there a way to fix this issue?

    Thanks in advance.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Yes, you can place your script at or near the end of the body, just before the adsense script so that it runs before the adsense gets to screw with things.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. Actually I hate those script ads placed inside body area. They are not suitable for unobtrusive javascript techniques. Is there another comprimise which can solve the problem without breaking unobtrusive javascript rules?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    You could set a timeout that frequently checks if the element is available, then sets an event to that element. That's about the only other way.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if this'll work, but you could also create the adsense script element dynamically, after your DOMContentLoaded stuff.
    Code Javascript:
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.appendChild(document.createTextNode(adsense)); // string holding adsense code
    script.src = 'http://google.com/adsense.js';
    document.body.appendChild(script);
    I don't use adsense so I'm not sure if you need to put some javascript directly into the script element or if it uses the src attribute, but I'm sure you know.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •