On my site, I have the option to close and expand a div, #ad, and I am trying to set it up so that it saves it if you close the div, so that it doesn’t reopen when you go to a new page.
Here’s my code so far:
// Setup toggle function.
function toggle() {
// Initialize our object data from our XHTML divs.
var ad = document.getElementById('biglink');
var link = document.getElementById('toggle');
if (ad.style.display == 'block') {
ad.style.display = 'none';
link. innerHTML = "expand";
document.cookie = "ad=closed";
} else {
ad.style.display = 'block';
link. innerHTML = "contract";
document.cookie = "ad=open";
}
}
// Original function by Duncan Crombie: dcrombie@chirp.com.au
// Please acknowledge use of this code by including this header.
var cook = document.cookie;
function getCookie(name) { // use: getCookie("name");
var index = cook.indexOf(name + "=");
if (index == -1) return null;
index = cook.indexOf("=", index) + 1;
var endstr = cook.indexOf(";", index);
if (endstr == -1) endstr = cook.length;
return unescape(cook.substring(index, endstr));
}
// Setup based on the cookie
function setup () {
if(getCookie('ad')=="closed") {
//alert("close");
}
}
document.onload = setup();
It’s able to set and read the cookie correctly, as the commented out alert works correctly. However, when I try to replace the alert with toggle(), it fails miserably.
Firefox’s javascript console reports:
“Error: ad has no properties”
It also mentions line 8, so toggle seems to only work onClick…
Is there any reason why this isn’t working that I’m overlooking?
Thanks a bunch,
Jeff