javascript remove banner after 30 days

Discussion in 'JavaScript' started by dirtydoug, Sep 15, 2009.

  1. #1
    Hello can someone help me to make a very simple script, it must be 1 file only using javascript. I want to display a banner on a period of time lets say 30 days and after that it will be replaced by a default banner. Just a simple script pls.

    Example:
    var ad1 = banner1.gif //banner 1
    var exp = 1/1/2009 //
    var addefault = defaultbanner.gif //default banner

    If todays_date < exp
    show ad1
    else
    show addefault

    :D i dont know how to elaborate so pls. try to understand.

    also is it possible to insert adsense on a javascript? cause if its possible i want the default banner to be my adsense. i think the solution is something like document.write("<iframe>"); but im still looking for another solution.
     
    dirtydoug, Sep 15, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    if your objective is for each new visitor to view one banner for 30 days then change the the default then you need to set a cookie, recording the original date of the visit and read it upon display, then calculate the difference in days and swap it to the default if > 30.

    you can embed adsense via javascript but it is probably fiddly. what I tend to do is this:

    output adsense as per usual but into a div that's styled with display: none. i then use javascript to move it and position it where I want it to be. you can just not show it until the 30 days elapse and then move it.

    anyway - good luck - this would be far easier a task to do via a framework that has a decent set of operations for date and time functions + dom manipulations. eg, mootools or jquery (although the latter would require some plugins)
     
    dimitar christoff, Sep 16, 2009 IP
  3. dirtydoug

    dirtydoug Member

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    oh my god you made it ...:) you made it more complicated for my usual brain to think hahah :D i am only looking for simple solution not complex explanation but i really really appreciate your time and effort to explain but somehow it wont register to my <=10 iQ haha hope someone would come out and give a little piece of or sample code that i wish to do.

    thanks
     
    dirtydoug, Sep 16, 2009 IP
  4. dirtydoug

    dirtydoug Member

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    i think im quite near now..

    <html>
    <head>
    </head>
    <body>
    <script language="JavaScript">
      <!--
        var now = new Date();  
        function fourdigits(number)	{
          return (number < 1000) ? number + 1900 : number;}
        m =  now.getMonth();
        d =  now.getDate();
        y =  (fourdigits(now.getYear()));
    expm = "8";
    expd = "16";
    expy = "2008"
    
    begd = m+""+d+""+y;
    expird = expm+""+expd+""+expy;
    
    if (begd == expird)
    {
         document.write("not expired <div>");
    }
    else
    {
       document.write("expired <div style='display:none;'>");
    }
    
      //-->
    
    </script>
    
    <script type="text/javascript"><!--
    google_ad_client = "pub-5922334097296258"; //sample adsense code only
    /* 728x90, created 5/2/09 */
    google_ad_slot = "4700919887"; //sample adsense code only
    google_ad_width = 728; //sample adsense code only
    google_ad_height = 90; //sample adsense code only
    //--> 
    </script>
    <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script><script src="http://pagead2.googlesyndication.com/pagead/expansion_embed.js"></script>
    </div>
    
    </body>
    </html>
    
    
    Code (markup):
    but the only thing is that i want to make sure that it wont destroy any unclose DIV tag on the page which i will insert this, i hope im making sense and about the date how can i ever set a # of days for before expiration cause i think my code is kinda complicated.
     
    dirtydoug, Sep 16, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    i am on a train at the moment but i wrote this up for you, hope it helps

    
    <html>
    <body>
    
    <script type="text/javascript">
    
    (function() {
        // run in closure to avoid namespace conflicts
    
        // define adverts
        var adverts = [
            {
                image: "http://i289.photobucket.com/albums/ll218/dperkins3/HasMag_Banner_728x90.jpg", // first 30 days
                url: "http://www.google.com"
            },
            {
                image: "http://i87.photobucket.com/albums/k160/xsk8apunk69/FD_R7_728x90.gif", // default
                url: "/members.php"
            }
        ];
    
        // wrapper for working with cookies.
        var Cookie = {
            set: function(c_name, value, options) {
                var exdate = new Date();
                exdate.setDate(exdate.getDate()+options.duration);
                document.cookie=c_name+ "=" +escape(value)+ ((options.duration==null) ? "" : ";expires="+exdate.toGMTString()) + ((options.path==null) ? "" : ";path="+options.path);
            },
            get: function(c_name) {
                if (document.cookie.length>0) {
                    var c_start=document.cookie.indexOf(c_name + "=");
                    if (c_start!=-1){
                        c_start = c_start + c_name.length+1;
                        var c_end = document.cookie.indexOf(";",c_start);
                        if (c_end==-1)
                            c_end=document.cookie.length;
                        return unescape(document.cookie.substring(c_start,c_end));
                    }
                }
                return false;
            },
            remove: function(name) {
    
            }
        };
    
        var getDays = function(dateString, nowString) {
            // calc days elapsed between two date strings
            var minutes = 1000 * 60, hours = minutes * 60, days = hours * 24;
            var t = Date.parse(dateString), t2 = Date.parse(nowString);
            return Math.round(t2/days - t/days);
        };
    
    
        // public function
        var returnAdvert = function(numDays) {
            var today = new Date();
    
            // cookie - try to get it.
            var startDate = Cookie.get("startDate");
    
            // not found, 1st visit, set the original date for first visit.
            if (startDate === false) {
                Cookie.set("startDate", today, {duration:365,path:"/"});
                startDate = Cookie.get("startDate");
            }
    
            var daysPassed = getDays(startDate, today);
    
    
            // pick initial advert or the end of the period one
            return (daysPassed > numDays) ? adverts[1] : adverts[0];
        };
    
        var advertObj = returnAdvert(30); // get the right advert after 30 days
    
        // example output
        document.write("<a href='"+advertObj.url+"'><img src='"+advertObj.image+"' border='0' /></a>");
    })();
    
    
    
    </script>
    
    </body>
    </html>
    
    PHP:
    need to polish it a little bit and test in IE but its ok for a start


    http://fragged.org/dev/cookieAdvertDisplay.html
     
    Last edited: Sep 17, 2009
    dimitar christoff, Sep 17, 2009 IP
  6. dirtydoug

    dirtydoug Member

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    hi thanks for this code i really appreciate jsut 2 more questions. how can i make the default banner my adsense banner? and also how to set a start date and end date?
    thanks
     
    dirtydoug, Sep 18, 2009 IP