Results 1 to 4 of 4

Thread: Switch Content body onload

  1. #1
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Content body onload

    I'm using:
    http://www.dynamicdrive.com/dynamici...tchcontent.htm

    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.

    Thanks
    Carol
    Last edited by CarolSvenson; 12-07-2005 at 12:41 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

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

    Code:
    <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():

    Code:
    function do_onload(){
    uniqueidn=window.location.pathname+"firsttimeload"
    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)
    revivecontent()
    }
    if (ccollect.length>0 && statecollect.length>0)
    revivestatus()
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thank you

    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!
    Carol
    Last edited by CarolSvenson; 12-10-2005 at 10:09 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    PLEASE: Include the URL to your problematic webpage that you want help with.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •