Here's a very rough prototype I adapted from my book "JavaScript: Just the Basics." When you load Test.html, it looks for the FormerLocation cookie. If it finds it, it automatically navigates to that page. Test.html has a link to Test01.html. If you go to Test01.html, the script saves the current URL in the FormerLocation cookie. It saves the cookie for 1 hour, but you can change the lifetime to anything you want.
Test.html:
Code:
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<script>
var FormerLocation = getFormerLocationCookie ();
if (FormerLocation)
window.location = FormerLocation;
function getFormerLocationCookie ()
{
/* get all cookies: */
var allCookies = document.cookie;
if (allCookies === "")
return null;
/* break up all-cookie string into an array: */
var cookieList = allCookies.split ("; ");
/* search through the array for the FormerLocation cookie: */
for (var i = 0; i < cookieList.length; ++i)
{
var cookie = cookieList[i];
var idx = cookie.indexOf ("=");
/* extract cookie name: */
var name = cookie.substring (0, idx);
if (name === "FormerLocation")
{
/* correct cookie found: extract, decode, & return cookie value: */
var value = cookie.substring (idx+1);
return decodeURIComponent (value);
}
}
return null;
}
</script>
</head>
<body>
<h2>Home Page</h2>
<a href="Test01.html">Go to Test01</a>
</body>
</html>
Test01.html:
Code:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tester Page</title>
<script>
/* for IE only: */
var date = new Date();
date.setHours (date.getHours()+1);
document.cookie = "FormerLocation=" + encodeURIComponent (window.location) + "; max-age=3600; expires=" + date.toGMTString();
</script>
</head>
<body>
<h2>JavaScript Tester Page</h2>
</body>
</html>
Bookmarks