Forum Moderators: open
Only Javascript code needed plz, for this problem:-
==========================-------
i have 2 pages.
page1.htm
page2.htm
if a visitor came first time to page1.htm, i want to redirect him to page2.htm and set a cookie on his browser that if he come again on the page1.htm he will not get redirected
(NOTE : i don't have problem, if he flush cookie and again get redirection, so leave this problem)
i just need to do this thing first.
waiting for your fast reply.
thans for reading.
regards.
// *** COOKIE FUNCTIONS ***
// cookies.js// -- setCookie()
// The cookieName and cookieValue arguments are mandatory but all other arguments are optional.
// expires - (optional) is a Date object.
// path - the part of the document tree on the server that the cookie is valid for.
// domain - allows multiple server hosts to be used.
// secure - boolean and only applicable for use with HTTPS: connections.
// Example...
// var today = new Date();
// var expires = new Date(today.getMilliseconds() + (10 * 86400000)); // +10 days
// setCookie('foo', 'bar', expires, '/');
function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
document.cookie = escape(cookieName) + "=" + escape(cookieValue)
+ ((expires)? "; expires=" + expires.toGMTString() : "")
+ ((path)? "; path=" + path : "")
+ ((domain)? "; domain=" + domain : "")
+ ((secure)? "; secure" : "");
}// -- getCookie()
// eg. myVar = getCookie("foo");
// Returns null if no cookie
function getCookie(cookieName) {
var cookie = " " + document.cookie;
var search = " " + escape(cookieName) + "=";
var setStr = null;
var offset = 0;
var end = 0;
if (cookie.length > 0) {
offset = cookie.indexOf(search);
if (offset!= -1) {
offset += search.length;
end = cookie.indexOf(";", offset)
if (end == -1) {
end = cookie.length;
}
setStr = unescape(cookie.substring(offset, end));
}
}
return(setStr);
}
Then, in the <head>...</head> section of page1.htm you need to include "cookies.js" and read the cookie. If it doesn't have the correct value, redirect to page2.htm:
<html>
<head>
<title>page1.htm</title>
<script type="text/javascript" src="cookies.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
var cookie = getCookie('mycookie');
if (cookie!= 'myvalue') {
window.location.href = "page2.htm";
}
//-->
</script>
</head>
<body>
etc....
</body>
</html>
Then, in page2.htm (again in the <head> section) you include "cookies.js" (as before) and call the setCookie() function to set the cookie to the value you want ('myvalue' in this case):
<html>
<head>
<title>page2.htm</title>
<script type="text/javascript" src="cookies.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
// Cookie expires at end of session (ie. when browser closes)
setCookie('mycookie','myvalue');
}
//-->
</script>
</head>
<body>
etc....
</body>
</html>
As I've mentioned in the comment above - the cookie expires when the browser is closed. If you want to make the cookie last longer (eg. 10 days) then you need to pass a 3rd argument to the setCookie() function. This is a JavaScript date object. Check out the comments in "cookies.js" for details on setting this.
Hope that is what you are after.