Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Location
    Buffalo, NY, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combining a conditional with an unconditional body onload

    Ok, here's the problem. I have two javascripts, each using the body onload event. Now usually, I'd just do this, but in this case the trouble is that one of the window.onload's is conditional, i.e. within an if () statement. Here's the code snippet demonstratiung how it appears in the head portion of my page:

    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 (window.addEventListener)
    window.addEventListener("load", do_onload, false)
    else if (window.attachEvent)
    window.attachEvent("onload", do_onload)
    else if (document.getElementById)
    window.onload=do_onload
    How do I solve this so that if needed, both onloads are executed and otherwise only the other one (a regular unconditional onload)?

    Thanks!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Add the other onload at the end of do_onload() function:

    Code:
    function do_onload(){
      ...
      ...
      otherOnload();
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Location
    Buffalo, NY, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok. The second onload is in the body. Does the script in the head know of the script in the body before the body is loaded (assuming the page is loaded sequentially)?

    To save some time, here is all the relevant code:

    Header:
    Code:
    <script type="text/javascript">
    
    /***********************************************
    * Switch Content script- © Dynamic Drive (www.dynamicdrive.com)
    * This notice must stay intact for legal use. Last updated Mar 23rd, 2004.
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
    var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)
    
    var contractsymbol='- ' //HTML for contract symbol. For image, use: <img src="whatever.gif">
    var expandsymbol='+ ' //HTML for expand symbol.
    
    
    if (document.getElementById){
    document.write('<style type="text/css">')
    document.write('.switchcontent{display:none;}')
    document.write('</style>')
    }
    
    function getElementbyClass(rootobj, classname){
    var temparray=new Array()
    var inc=0
    for (i=0; i<rootobj.length; i++){
    if (rootobj[i].className==classname)
    temparray[inc++]=rootobj[i]
    }
    return temparray
    }
    
    
    function contractcontent(omit){
    var inc=0
    while (ccollect[inc]){
    if (ccollect[inc].id!=omit)
    ccollect[inc].style.display="none"
    inc++
    }
    }
    
    function expandcontent(curobj, cid){
    var spantags=curobj.getElementsByTagName("SPAN")
    var showstateobj=getElementbyClass(spantags, "showstate")
    if (ccollect.length>0){
    if (collapseprevious=="yes")
    contractcontent(cid)
    document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
    if (showstateobj.length>0){ //if "showstate" span exists in header
    if (collapseprevious=="no")
    showstateobj[0].innerHTML=(document.getElementById(cid).style.display=="block")? contractsymbol : expandsymbol
    else
    revivestatus()
    }
    }
    }
    
    function revivecontent(){
    contractcontent("omitnothing")
    selectedItem=getselectedItem()
    selectedComponents=selectedItem.split("|")
    for (i=0; i<selectedComponents.length-1; i++)
    document.getElementById(selectedComponents[i]).style.display="block"
    }
    
    function revivestatus(){
    var inc=0
    while (statecollect[inc]){
    if (ccollect[inc].style.display=="block")
    statecollect[inc].innerHTML=contractsymbol
    else
    statecollect[inc].innerHTML=expandsymbol
    inc++
    }
    }
    
    function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
    offset += search.length
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function getselectedItem(){
    if (get_cookie(window.location.pathname) != ""){
    selectedItem=get_cookie(window.location.pathname)
    return selectedItem
    }
    else
    return ""
    }
    
    function saveswitchstate(){
    var inc=0, selectedItem=""
    while (ccollect[inc]){
    if (ccollect[inc].style.display=="block")
    selectedItem+=ccollect[inc].id+"|"
    inc++
    }
    
    document.cookie=window.location.pathname+"="+selectedItem
    }
    
    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 (window.addEventListener)
    window.addEventListener("load", do_onload, false)
    else if (window.attachEvent)
    window.attachEvent("onload", do_onload)
    else if (document.getElementById)
    window.onload=do_onload
    
    if (enablepersist=="on" && document.getElementById)
    window.onunload=saveswitchstate
    
    </script>
    Body:
    Code:
    <script type="text/javascript">
    
    /******************************************
    * Scrollable content script II- © Dynamic Drive (www.dynamicdrive.com)
    * Visit http://www.dynamicdrive.com/ for full source code
    * This notice must stay intact for use
    ******************************************/
    
    iens6=document.all||document.getElementById
    ns4=document.layers
    
    //specify speed of scroll (greater=faster)
    var speed=5
    
    if (iens6){
    document.write('<div id="container" style="position:relative;width:175px;height:160px;border:1px solid black;overflow:hidden">')
    document.write('<div id="content" style="position:absolute;width:170px;left:0;top:0">')
    }
    </script>
    
    <ilayer name="nscontainer" width=175 height=160 clip="0,0,175,160">
    <layer name="nscontent" width=175 height=160 visibility=hidden>
    
    <!--PLAIN HTML CONTENT HERE-->
    </layer>
    </ilayer>
    
    <script language="JavaScript1.2">
    if (iens6)
    document.write('</div></div>')
    </script>
    
    <table width="175px"><td><p align="right">
    <a href="#" onMouseover="moveup()" onMouseout="clearTimeout(moveupvar)"><img src="up.gif" border=0></a>  <a href="#" onMouseover="movedown()" onMouseout="clearTimeout(movedownvar)"><img src="down.gif" border=0></a></p></td>
    </table>
    
    <script language="JavaScript1.2">
    if (iens6){
    var crossobj=document.getElementById? document.getElementById("content") : document.all.content
    var contentheight=crossobj.offsetHeight
    }
    else if (ns4){
    var crossobj=document.nscontainer.document.nscontent
    var contentheight=crossobj.clip.height
    }
    
    function movedown(){
    if (iens6&&parseInt(crossobj.style.top)>=(contentheight*(-1)+100))
    crossobj.style.top=parseInt(crossobj.style.top)-speed+"px"
    else if (ns4&&crossobj.top>=(contentheight*(-1)+100))
    crossobj.top-=speed
    movedownvar=setTimeout("movedown()",20)
    }
    
    function moveup(){
    if (iens6&&parseInt(crossobj.style.top)<=0)
    crossobj.style.top=parseInt(crossobj.style.top)+speed+"px"
    else if (ns4&&crossobj.top<=0)
    crossobj.top+=speed
    moveupvar=setTimeout("moveup()",20)
    
    }
    
    function getcontent_height(){
    if (iens6)
    contentheight=crossobj.offsetHeight
    else if (ns4)
    document.nscontainer.document.nscontent.visibility="show"
    }
    window.onload=getcontent_height
    </script>

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Yes because even if it's in the head, the function is called when the document has loaded. So just call getcontent_height() at the end of do_onload().
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New to the CF scene
    Join Date
    Mar 2005
    Location
    Buffalo, NY, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm... for some reason it still does not seem to be doing things right. I've uploaded the whole file here. What I'd like to see is that when you expand an item within the box the scroll mechanism adjusts. Is there a simple way to fix this?

    Thanks for your help, by the way, it is much appreciated.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    It's doing things right, it's doing what it's scripted to do. What you're expecting is not in the features of the 2 scripts you combined.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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