Reputation: 536
I'd like to perform an animation on my website only if it is the user's first time on the website. I found the "first time visitor" code with localStorage
, but I didn't get to find about this.
Upvotes: 7
Views: 17244
Reputation: 51
In my opinion, I think it is best you use a database to track such operations. Using local storage or cookies are browser specific. Ones the user changes his or her browser, the feature has to start again. However with databases, there is a form of persistence. Here is what you can do with database.
Upvotes: 0
Reputation: 99
I'd do the following. You won't need cookies here. Just check whether the document.referrer includes your domain. If not, the visitor just came to your domain without visiting further pages.
if(document.referrer.indexOf("yourdomain.com")==-1) {
//first pageview of the session
}
You can also set a cookie in addition, just in case the document.referrer gets lost somewhere.
Upvotes: 9
Reputation: 14297
I am providing a sample code for @Joseph the Dreamer approach
Create and Store a Cookie
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
Get a Cookie Value
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
Upvotes: 1
Reputation:
You can use either cookie
or localStorage
. Consider you have the following HTML code:
<div id="first">
Welcome Visitor
</div>
<p>This is my website.</p>
We set the display
of first
element to none
. So it will never show.
#first {
display: none;
}
Then we check for the first time. If it is the first time, we show that element (and maybe do some animation with it):
// this is the first time
if (! localStorage.noFirstVisit) {
// show the element
// and do the animation you want
document.getElementById('first').style.display = 'block';
// check this flag for escaping this if block next time
localStorage.noFirstVisit = "1";
}
Upvotes: 10
Reputation: 1907
assuming you are meaning first page viewed in this window:
if (location.referrer == location.href)
{
// the page was opened in a new window or tab and accessed directly
}
else if (location.referrer.indexOf(location.hostname) == -1)
{
// The page before this one was not a page on this site
}
Upvotes: 0
Reputation: 119847
When the visitor visits your site, set a cookie that indicates that they have. On every page load, check if that cookie exists. If it does, then the user has been to your site before. If it doesn't exist, it's either his first visit... or they have deleted your cookie from their browser.
So if it is my first visit, the cookie shouldn't be there, and you should play the animation.
Upvotes: 2