Reputation: 21
How to redirect User in No.1 html page to another No. 2 html page using javascript when a user visit first time in No.1 page. If possible pls help me.
Upvotes: 2
Views: 93
Reputation: 6537
Agree with legotin, but you will need to read/write cookie somehow:
Read cookie:
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
Set cookie:
function setCookie(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires == "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires * 1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for (var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
}
Upvotes: 0
Reputation: 23565
To check 1st visit or not one can use cookie or localStorage.
And to redirect there are several methods/mechanisms: this article explains it well
Upvotes: 0
Reputation: 2688
Just use cookie to mark the user first time and redirect it with document.location
:
if ( getCookie( 'first-time' ) === undefined ) {
setCookie( 'first-time', 'yes' );
document.location = '/second-page-url';
}
To use cookie read this Set cookie and get cookie with JavaScript and this Get cookie by name
Upvotes: 1
Reputation: 9391
You need to set a cookie on the first visit, then you can check the user was there before on your site.
Upvotes: 0