# Javascript Hide onload

**URL:** <https://forum.kirupa.com/t/javascript-hide-onload/157535>\
**Category:** web dev\
**Created:** [August 2, 2005, 4:43pm UTC](https://forum.kirupa.com/t/javascript-hide-onload/157535 "2005-08-02T16:43:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [August 2, 2005, 4:43pm UTC](https://forum.kirupa.com/t/javascript-hide-onload/157535/1 "2005-08-02T16:43:50Z")

</div>

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:

```auto
// 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
