Reputation: 2942
I am setting a welcome message for my website that should be shown at the first time that users enter the website. So I display a specific element for some seconds when first page loads. The problem is each time that users go to homepage of the site would see the welcome message. Is it a way that I check if this is the first time that user opens homepage? I have no server-side code and every thing I have is javascript
Upvotes: 0
Views: 1090
Reputation: 941
You can set a browser cookie when the site is visited for the first time. Read the cookie. If cookie is available it means site is already visited. If cookie is not there it means site is visited for the first time and you have to display your welcome message.
javascript
window.onload = function() {
var visit=GetCookie("COOKIE1");
if (visit==null){ //show your custom element on first visit
alert("Welcome new user");
}
var expire = new Date();
expire = new Date(expire.getTime()+7776000000);
document.cookie = "COOKIE1=here; expires="+expire;
};
for further reference also see: http://www.htmlgoodies.com/legacy/beyond/javascript/cookiecountexplanation.html
jQuery
<script type="text/javascript">
$(document).ready(function() {
// check cookie
var visited = $.cookie("visited")
if (visited == null) { //first visit
$('.custom_element').show(); //show your custom element on first visit
alert("Welcome new user");
$.cookie('visited', 'yes');
}
// set cookie
$.cookie('visited', 'yes', { expires: 1, path: '/' });
});
</script>
Upvotes: 2