0

Guys how can I check when the users first visited the website? because I want to display a pop up message when he/she first visited the website.,I found this question and this website http://www.electrictoolbox.com/jquery-cookies/ but I don't know how to use it. I just wrote a simple code in order for me to check how it is done.

<script type="text/javascript">

$.cookie("example", "foo");
alert( $.cookie("example") );

</script>

but its not working. What I am doing wrong here? or maybe you can suggest for another method. Any help would be much appreciated. Thanks.

Community
  • 1
  • 1
xknozi
  • 1,335
  • 3
  • 12
  • 20

3 Answers3

3

You first need to check whether the cookie exists, and if so do the message (please don't use alert for that), and then set the cookie. E.g.:

if (!$.cookie("yourcookie")) {
    // Show a message (please don't use alert)
}
$.cookie("yourcookie", "anything not blank here");

Of course, this only checks that the user doesn't have the cookie, it doesn't necessarily mean they've never been to the site before (as users can clear cookies).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1
  1. download the js from this URL https://github.com/carhartl/jquery-cookie and import into your code
  2. you can set cookie $.cookie("example", "foo"); OR ($.cookie("example", "foo", { expires: 7 }); - cookie last for 7 days )
  3. you can retrive the cookie by $.cookie("example");

using these

if(!$.cookie("example"))
{
 alert('not 1st time');
}
{
 alert('1st time');
 $.cookie("example", "foo"); //set the cookie
}
Techie
  • 44,706
  • 42
  • 157
  • 243
0

On index page check if some cookie exist, if not set cookie to indefinite time and next time user returns to page you will know that he already visited (if he didn't remove cookies from browser). You could also put his IP address with some other data in your DB and check from DB if user has visited before, but it is also unreliable because there are ways you can change your IP also, depends on what are you trying to achieve and how important it is to know if user has visited page before.

v0d1ch
  • 2,738
  • 1
  • 22
  • 27