SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to show popup only once through cookies

    I want to show popup only once, and I have already written code for it but it is not working.

    I want to execute did_you_know_slide_in() function only once.

    Can anyone guide me what i am doing wrong.


    HTML Code:
    function get_cookie(Name) {
      var search = Name + "="
      var returnvalue = "";
      if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) { // if cookie exists
          offset += search.length
    
          end = document.cookie.indexOf(";", offset);
    
          if (end == -1)
             end = document.cookie.length;
          returnvalue=unescape(document.cookie.substring(offset, end))
          }
       }
      return returnvalue;
    }
    
    function loadornot(){
        if (get_cookie('hasSeen')==''){
            document.cookie="hasSeen=yes"
            did_you_know_slide_in();
        }
    }
    
    loadornot();

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code works so something else must be wrong.

    Any console errors?

    Are you sure you're deleting all cookies for the domain?

    A useful bookmarklet is javascript:alert(document.cookie);

    Your cookie reader doesn't check that the matched name is a whole word, so in a cookie string like peanuts=2;nuts=1 it would pick up the value of 'peanuts' when searching for 'nuts'.
    Code:
    function get_cookie(Name) 
    {
     var cString;
         
     return ( ( cString = document.cookie.match( "(^|;|\\s)" + Name + "=([^;$]+)" ) ) ) ? decodeURIComponent( cString[ 2 ] ) : "";
    }
    Tab-indentation is a crime against humanity.


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
  •