SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to apply cookie session to popup script

    Hello everyone,

    I found this neat looking script which appears very nicely on my site:

    http://www.dynamicdrive.com/dynamicindex11/abox.htm

    I've come to realize today that it doesn't actually have any cookie code so that it only appears once every visitor session, instead in it's current state it appears every page load which is incredibly annoying. I know absolutely nothing on how to write or read javascript, can anyone help to include the cookie code?

    Thank you v much!

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <style type="text/css">
    #showimage {
        display: none;
    }
    </style>
    <script type="text/javascript">
    
        window.onload = function() {
            if(readCookie('popupShown') != null) {
                document.getElementById('showimage').style.display = 'block';
                createCookie('popupShown','true', 365);
            }
        }
    
        function createCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        }
    
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }
    
        function eraseCookie(name) {
            createCookie(name,"",-1);
        }
    </script>
    Add a style to your css to hide the box initially, and then when the page loads, check if an cookie exists, if not show the popup and set the cookie.



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
  •