Blu Blu - 12 days ago 7
Javascript Question

Check if user has alread visited site in browser session

So I have this splash screen animation that plays when you visit my website. However, I only want the animation to play the first time you visit it (in the session of course). I tried to make an if else where I check for a cookie called "Session", and if it exists, delete the div containing the animation. If there is no such cookie, create it.

Code:

function session() {
if (document.cookie.indexOf("visited") >= 0) {
$('.splash').remove(this);
} else {
document.cookie = "visited";
}
}


I have never used cookies before so I think I might have made some errors

Answer

The portion of your code that deals with cookies should work.

Your problem exists on this line:

$('.splash').remove(this);

Remove this, as it's not necessary.

That line should look like:

$('.splash').remove();

Hope that helps.

Comments