Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Location
    Knoxville, TN
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help setting a cookie so the welcome box displays only ONCE

    Hi everyone,

    I am working with the "sexy alert box" found here and have the code below, which pops up the sexy alert box on every page load.

    Code:
    <script type="text/javascript">
    function test() {
    Sexy.alert('Hello world!');
    }
    window.onload=test;
    </script>
    My problem is that I only want that code to show on the FIRST load for the user and every page after that it should not be showing. This will be the first time I've worked with cookies so I am a bit confused as to what to do with this code and where to edit things.

    Any help I receive is greatly appreciated

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try this:-

    Code:
    <html>
    <head>
    <script type="text/javascript">
    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>
    </head>
    <body>
    <script type="text/javascript">
    if(!readCookie('wroteIt')){
    document.write('Your Message Goes Here And You See It Only Once');
    createCookie('wroteIt', 'wroteIt', 365);  // 365 days persistence
    }
    </script>
    </body>
    </html>
    You could have found another example not a million miles from here at:-
    http://www.javascriptkit.com/script/...alertmsg.shtml


    “Expert: a man who makes three correct guesses consecutively.”
    Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)
    Last edited by Philip M; 06-02-2009 at 07:57 AM.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Location
    Knoxville, TN
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Philip - thanks.

    I have a piece of code I've been working on but it's not working - any idea's why this wouldn't be working?

    Code:
    function firstVisit() {
        var isFirstVisit = false;
        if (!readCookie('firstVisit')) {
            createCookie('firstVisit', 'true', 365);
            isFirstVisit = true;
        }
        return isFirstVisit;
    }
    function test() {
        if (firstVisit()) {
    Sexy.alert('Hello world!');
        }
    }
    
    window.onload=test;

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    <script type="text/javascript">
    
    function addListener(obj, evt, handler)
    {
       if (obj.addEventListener)
       {
          obj.addEventListener(evt, handler, false);
       }
       else if (obj.attachEvent)
       {
          obj.attachEvent('on' + evt, handler);
       }
    }
    
    addListener(window, 'load', function()
       {
          if (!/__BEENSEXY__/.test(window.name))
          {
             Sexy.alert('Hello world!');
             window.name = '__BEENSEXY__';
          }
       }
    );
    
    </script>

  • #5
    New to the CF scene
    Join Date
    May 2009
    Location
    Knoxville, TN
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    adios - you're a genius! thank you so much


  •  

    Posting Permissions

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