View Full Version : Switch Content body onload

12-06-2005, 12:09 AM
I'm using:

and would like to know if there's any way to get the expansion to trigger on a body onload instead of a mouse click. This doesn't work:

<body onload="expandcontent ('sc1')">

I'm not good at javascript.


12-07-2005, 08:17 AM
Select the content that you want open onload and find its activating element. For the first piece of expandable content from the demo that would be:

<h3 onClick="expandcontent(this, 'sc1')" style="cursor:hand; cursor:pointer"><span class="showstate"></span>What is JavaScript?</h3>

Give it an id say, 'open':

<h3 id="open" onClick="expandcontent(this, 'sc1')" style="cursor:hand; cursor:pointer"><span class="showstate"></span>What is JavaScript?</h3>

Put this bit of code (highlighted red) at the very end of the function do_onload():

function do_onload(){
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
if (ccollect.length>0 && statecollect.length>0)
if (document.getElementById('sc1').style.display!='block')
expandcontent(document.getElementById('open'), 'sc1')

If you want to use a different section of expandable content, use its sc# designation and assign the id="open" to its activating element.

12-10-2005, 10:01 PM
John, for helping me out w/ this. I actually have 7 menu items (I'm using this script for menuing as it works better for what I'm trying to do than the other DD scripts I've tried.).

At any rate, each menu button has an id of sc1 thru sc7. I want to be able to trigger the expansion of any one of them when a page loads (individual pages will be bookmarked and when people go directly to a page I want that section's menu to open up).

The code you provided will only work for one id, but it gave me an idea.

Based on what you provided, I made a separate .js file for each section (1 thru 7):

function sectionOne() {
document.getElementById('sc1').style.display = "block";

Sections 2 thru 7 have their own function as well with an sc id. I call the .js files in the <head> section of my main files and in the body onload I then call the functions.

This works perfectly in Mozilla. When bookmarking a page and going directly to it the menus expand exactly where I want. But in IE they don't expand when the page loads.

Any ideas why this works in Mozilla/Netscape but not IE?

Many thanks again for all of your help!

12-10-2005, 11:04 PM
Off the top of my head, no. If you are happy with the way this works in Mozilla, I am assuming that you are not using expand and contract symbols (like the + and - of the demo) or that if you are, you don't mind that they do not change on page load.

To determine why what looks like valid code that also happens to be working in Mozilla is not working in IE, it would be a big help if I could see a live demo. Could you put one up of what you have that works in Mozilla? It wouldn't have to be linked to or from any of your other pages, just put it up and post the url here: