I'm using IE5 and I want to save a cookie, that records what the browser dimensions were when it closed. Then when you open that page, it can remember it. But for some reason it's only saving the first cookie name-value pair. The onLoad and onUnload seem to be working strangley too. Hope you can help! Here's the code:
<html>
<head>
<title>Page Size</title>
<link rel="stylesheet" type="text/css" href="../css/map.css">
<meta name="Author" content="Ben Weeks">
<script language="JavaScript">
<!-- // Hide from incompatible brosers
function getPageSize()
{
alert("boo!");
document.write("<p>" + document.cookie + "</p>");
HKMapWidth = getCookieData('HKMapWidth'
);
HKMapHeight = getCookieData('HKMapHeight
');
document.write("<p>HKMapWi
dth = " + HKMapWidth + ", HKMapHeight = " + HKMapHeight + "</p>");
}
function savePageSize()
{
alert("saving cookie!");
// Find the browser dimensions.
var browserWidth = document.body.offsetWidth;
var browserHeight = document.body.offsetHeight
;
// Set the expirey date one year from now. Must be in GMT format.
var exp = new Date();
var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000);
exp.setTime(oneYearFromNow
);
// If cookie already exists it will over-ride the old value, if it doesn't it will create a new cookie.
// NB See p308 in the JavaScript Bible 3rd Edition for more information.
document.cookie = "HKMapWidth=" + browserWidth + ";HKMapHeight=" + browserHeight + ";expires=" + exp.toGMTString();
}
function getCookieData(label)
{
// this function can be found on p311 of the JavaScript Bible 3rd Edition.
// More cookie retrieving functions can be found at:
//
http://www.hidaho.com/cookies/cookie.txt label = label + "="; // Assume cookie is in the format of cookieName=cookieValue, i.e. no spaces.
var labelLen = label.length;
var cookieLen = document.cookie.length;
var i = 0;
while (i < cookieLen)
{
var j = i + labelLen;
if (document.cookie.substring
(i,j) == label)
{
strEnd = document.cookie.indexOf(";
",j)
if (strEnd == -1)
{
strEnd = document.cookie.length;
}
return unescape(document.cookie.s
ubstring(j
,strEnd));
}
i++
}
return "";
}
// -->
</script>
<body onLoad="getPageSize()" onUnload="savePageSize()">
</body>
</html>