Try:
Code:
<html>
<head><title>send cookie</title>
<script type='text/javascript'>
var expdate = new Date();expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset); if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr)); }
function GetCookie (name) { var arg = name + "="; var alen = arg.length;
var clen = document.cookie.length; var i = 0; while (i < clen) {
var j = i + alen; if (document.cookie.substring(i, j) == arg)
return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break; } return null; }
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : ""); }
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thur, 01-Jan-70 00:00:01 GMT";
}
}
function set_href_cookie(href_val){
SetCookie('href_location', href_val, expdate, '/', null, false);
document.location.href=href_val;
}
</script>
</head>
<body onbeforeunload="set_href_cookie('href_value')">
<script type='text/javascript'>
cookie_name = GetCookie("href_location");
if(cookie_name){
alert("Cookie found, redirecting to stored cookie.");
document.location.href=cookie_name;
}
</script>
</body>
</html>
The only problem here however, is triggering setting the cookie. At the moment, I am using onbeforeunload to trigger it, so it triggers the cookie before they are about to leave the page (don't ask me how it works), however the only problem with this, is that it will trigger it when they are are about to leave any page on your website, However I have made it so that it overwrites the cookie each time, so that when they leave the page for good, it should read the cookie with the URL of the last page they were on.
Bookmarks