views:

347

answers:

2

I want create a div container with a message for first time visitors to my site. Visitors will read it and click a 'Hide' button to dissapear it forever.

How is this done?

+8  A: 

What about using the Cookie plugin for jQuery?

$.cookie('the_cookie', 'the_value'); // Create a session cookie ("the_cookie") and set its value to "the_value"
$.cookie('chocolate_chip_cookie', 'the_value', { // create a cookie with all available options
    expires: 7, // expires in seven days
    path: '/', // accessible from the whole site...
    domain: 'jquery.com',
    secure: true // ...but only on a secure connection
});
$.cookie('the_cookie', null); // delete the session cookie
moff
I am trying to use the cookie plugin without much luck.My code so far is:var alert = $.cookie('beenHereBefore');//if cookie has a value of 'yes', hide the alert otherwise if it doesn't have that value, display it and set a cookie value of 'yes' on click if (alert == 'yes') { $("#alert").hide(); } else { $("#alert").fadeIn("2000"); $("#alert").click(function(){ $(this).slideUp("400"); $.cookie('beenHereBefore', 'yes'); }); }It doesn't work,any thoughts?
Zander
I have my setup on JSBIN, would you mind checking it over. It seems to work, but I'm not sure I'm hiding the element properly and I don't know if the code could be optimised.http://jsbin.com/obutuMany thanks
Zander
Your code is all right, but if you want to eliminate that flicker appearing on subsequent visits, you'd probably better go with setting the CSS of #alert to display: none. jQuery will automatically show the element when you call fadeIn: http://jsbin.com/igube
moff
A: 

You can accomplish this by:

  1. Setting a cookie.
  2. Storing the status in a session.

You can do 1. with JavaScript but also with PHP. 2. can only be accomplished server-side with e.g. PHP.

This tutorial may be helpful: http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/

bart
Could I use the visitor's IP address instead?
Zander
How is this done with PHP then?
Zander