www.webdeveloper.com
Results 1 to 9 of 9

Thread: Alert Box w/cookie

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Posts
    8

    Arrow Alert Box w/cookie

    New to the forum so first off, hello!

    I have a code that shows an alert box only once when a user views a page. What I need is a cancel button that will bring the user back to the previous page they were on if clicked. If they press cancel they would then continue to recieve the pop-up.

    Thanks for the help!

    Code:
    <script>
    
    //Alert message once script- By JavaScript Kit
    //Credit notice must stay intact for use
    //Visit http://javascriptkit.com for this script
    
    //specify message to alert
    var alertmessage= "Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site."
    
    
    ///No editing required beyond here/////
    
    //Alert only once per browser session (0=no, 1=yes)
    var once_per_session=1
    
    
    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
          // set index of beginning of value
          end = document.cookie.indexOf(";", offset);
          // set index of end of cookie value
          if (end == -1)
             end = document.cookie.length;
          returnvalue=unescape(document.cookie.substring(offset, end))
          }
       }
      return returnvalue;
    }
    
    function alertornot(){
    if (get_cookie('alerted')==''){
    loadalert()
    document.cookie="alerted=yes"
    }
    }
    
    function loadalert(){
    	
    alert(alertmessage)
    }
    
    if (once_per_session==0)
    loadalert()
    else
    alertornot()
    
    </script>

  2. #2
    Join Date
    Feb 2010
    Posts
    184

  3. #3
    Join Date
    Jul 2010
    Posts
    8

    Redux

    Here is what I have now. It works great except that now the pop-up continues to show everytime you reload i.e the cookie element no longer functions.

    I know I am probably missing something basic here but I am just starting to learn java. Thanks!

    Code:
    <script>
    
    //Alert message once script- By JavaScript Kit
    //Credit notice must stay intact for use
    //Visit http://javascriptkit.com for this script
    
    //specify message to alert
    var alertmessage= confirm("Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site.")
    if (!alertmessage){
    		window.location = "javascript:history.go(-1)";
    	}
    
    ///No editing required beyond here/////
    
    //Alert only once per browser session (0=no, 1=yes)
    var once_per_session=1
    
    
    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
          // set index of beginning of value
          end = document.cookie.indexOf(";", offset);
          // set index of end of cookie value
          if (end == -1)
             end = document.cookie.length;
          returnvalue=unescape(document.cookie.substring(offset, end))
          }
       }
      return returnvalue;
    }
    
    function alertornot(){
    if (get_cookie('alerted')==''){
    loadalert()
    document.cookie="alerted=yes"
    }
    }
    
    function loadalert(){
    	
    alert(alertmessage)
    }
    
    if (once_per_session==0)
    loadalert()
    else
    alertornot()
    
    </script>

  4. #4
    Join Date
    Jun 2007
    Posts
    667
    Just drop this in somewhere:
    Code:
    <script type='text/javascript'>
    
    (function()
    {
      if( !/(^|\s|;)seenAlert=yes(;|\s|$)/.test( document.cookie ) )
      {
        if( !confirm('Stay here?') )
         history.back();
        else
         document.cookie="seenAlert=yes";
      }
    
    })();
    
    </script>

  5. #5
    Join Date
    Jul 2010
    Posts
    8

    Redux

    That doesn't seem to do it. I discovered that by just adding the "confirm" to the code, the cookie code does not work.

  6. #6
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by tmik View Post
    That doesn't seem to do it. I discovered that by just adding the "confirm" to the code, the cookie code does not work.
    I don't know what you mean by that. Presumably you're testing it under the right circumstances, namely the link to the page using the script isn't opening in a new window and isn't being loaded by location.replace. Here's an amendment that tries to return to the referring page if there is no history available. That part won't work locally.:
    Code:
    <script type='text/javascript'>
    
    (function()
    {
      if( !/(^|\s|;)seenAlert=yes(;|\s|$)/.test( document.cookie ) )
      {
        if( !confirm('Stay here?') )
        {
         history.back();
         location.href = document.referrer;
        }
        else
         document.cookie="seenAlert=yes";
      }
    
    })();
    
    </script>
    http://veryuseful.info/test/stayorgo.htm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles