SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    refreshing a script tag (ads)

    As a site I work on evolves, we ajax in more and more content.

    In one section, we've eliminated the traditional page view entirely, just ajaxing in all the new content.

    We need to do the same thing for the ads surrounding this content, and we're running into issues (doc.write, I assume). We haven't been able to refresh the script tag, or even empty the div and add in a new script.

    We're iframing for the moment, but it is not ideal. Many of our ads are flexible, but the iframes cannot dynamically adjust (all the ads are hosted externally). This is not against our TOS (before someone chimes in about that).

    Search engines haven't been much help here. I'm not hitting the right words, or there just aren't many examples out there.

    I'm pretty sure this is possible, I know I've seen some sites do it (but I haven't been able to find them again for examples).

    Does anyone have any suggestions or examples?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    A simple thing you could do is number all the ads and wrap them in divs for example

    HTML Code:
    <div id="ad_1"></div>
    <div id="ad_2"></div>
    then through a for loop you could set an interval of say 60 seconds or something then the ads refresh. Here's something that could help

    Code JavaScript:
    function refreshAdvertisements(){
        for(var i=0; i<2; i++){
            var newAd = getNewRandomAd();
            $('#ad_'+i).html(newAd);
        }
    }
     
    setInterval("refreshAdvertisements()", 60000);

    Of course you would have to adjust this to your liking but it should give you a basic idea of how to can change the ads with Ajax

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I was doing something similar, but when using <script> tags inside the .html() (yup, trying with jQuery, too), nothing happened. Do I need to "activate" those script tags?
    Last edited by jjjamiem; Aug 4, 2010 at 03:56. Reason: shared comp

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There's a new version of the innerHTML() function which is called innerXHTML(), apparently its more efficient and works a lot better then both innerHTML() and jQuery's .html() function. You can read more about it here

    http://www.stevetucker.co.uk/innerxhtml.htm


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
  •