troubleshooting Question

Problem with script when #top is in the URL

Avatar of blackled
blackled asked on
JavaScript
5 Comments1 Solution319 ViewsLast Modified:
Hello the code i am using below has one problem that i haven't been able to resolve - If i have a url using the #top anchor (http://www.domain.com/theurl.htm#top), the script trashes.  This problem is only present if the user does a refresh of the page when it contains the #top anchor.

I am looking for a solution.  The favoured option would be to disregard "theurl.htm#top", but still allow for it to work if clicked on the bottom buttons (these take the user to the top of the page and open the right content body).  The second option (if the first can't be done) would be to have the "theurl.htm#top" open body1.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test</title>
<style>
html {
     scrollbar-face-color: #FFFFFF;
     scrollbar-track-color: #FFFFFF;
     scrollbar-arrow-color: #003366;
     scrollbar-highlight-color: #003366;
     scrollbar-3dlight-color: #FFFFFF;
     scrollbar-shadow-color: #FFFFFF;
     scrollbar-darkshadow-color: #003366;
     font-family: verdana, arial, helvetica, sans-serif;
     font-size: 76%;
}

/* pages styles */
body {
     margin: 0px;
     padding: 0px;
}

#header {
     margin-right:10px;
     margin-left:10px;
     margin-top:10px;
     border:1px solid #003366;
     padding:0px;
     background-image: url(../images/header-bg.jpg);
     height:100px;
}

#header p,#footer p {
     margin:0px;
     padding:0px;
}

       
p, li {
     font:11px/20px verdana, arial, helvetica, sans-serif;
     margin:0px 0px 16px 0px;
     padding:0px;
     }

ul {
     text-align:left;
}

ul li, ul ul li {
     margin-bottom:0px;
}
     
#Content>p {margin:10px;}

#content a {
     color:#09c;
     font-size:11px;
     text-decoration:none;
     }
#content a:link{color:#09c;}
#content a:visited {color:#07a;}

/* All the content boxes belong to the content class. */
#content{
      margin-right:10px;
      margin-left:10px;
      margin-top:10px;
      padding:10px;
      border:1px solid #003366;
      text-align:justify;
}


h1 {
      margin:0px 0px 15px 0px;
      padding:0px;
      font-size:28px;
      font-weight:bold;
      color:#FF9900;
}


/*contentnav*/
#cbody1, #cbody2, #cbody3, #cbody4, #cbody5 {
      margin-top:38px;
      margin-right:0px;
      margin-left:0px;
      padding:10px;
      border:1px solid #aaa;
      text-align:justify;
}

#cbody0tab, #cbody1tab, #cbody2tab, #cbody3tab, #cbody4tab, #cbody5tab {
      position:absolute;
      list-style:none;
      text-align:center;
      width:85px;
      top:180px;
}

#cbody0tab {
      left:20px;
}

#cbody1tab {
      left:107px;
}

#cbody2tab {
      left:194px;
}

#cbody3tab {
      left:281px;
}

#cbody4tab {
      left:368px;
}


#cbody0tab a, #cbody1tab a, #cbody2tab a, #cbody3tab a, #cbody4tab a, #cbody5tab a {
      width:85px;
      color:#444;
      text-decoration:none;
      display:block;
      padding-top: 2px;
      padding-bottom: 2px;
}

#bmenu {
      margin-left:0px;
}

#theprice {
      list-style:none;
      font-size: 18px;
      color:#09c;
      line-height: 16px;
}

#cbody0btab, #cbody1btab, #cbody2btab, #cbody3btab, #cbody4btab, #cbody5btab {
      list-style:none;
      text-align:center;
      float:left;
      width:85px;
      border-bottom:1px solid #aaa;
      margin-top:-5px;      
}

#cbody0btab {
      margin-left:-40px;
}

/* fix for IE */
*html #cbody0btab {
      margin-left:0px;
}

#cbody0btab a, #cbody1btab a, #cbody2btab a, #cbody3btab a, #cbody4btab a, #cbody5btab a {
      width:85px;
      color:#444;
      text-decoration:none;
      display:block;
      padding-top: 2px;
      padding-bottom: 2px;
}

.notselected {
      background:#ddd;
      border-top:1px solid #fff;
      border-left:1px solid #fff;
      border-right:1px solid #aaa;
}

.notselected a:hover {
      background:#AFCAE4;
}

.selected {
      background:#fff;
      border-top:1px solid #aaa;
      border-left:1px solid #aaa;
      border-bottom:1px solid #fff;
      border-right:1px solid #aaa;
}
</style>
<script language="javascript" type="text/javascript">
/* Tab content navigation function */
function switchid(id){
     ids = document.getElementById('content').getElementsByTagName('div');    
     hideallids();
     showdiv(id);
}

function hideallids(){
     //loop through the array and hide each element by id
     for (var i=0;i<ids.length;i++){
          hidediv(ids[i].id);    
     }            
}

function hidediv(id) {
     //safe function to hide an element with a specified id
     if (document.getElementById) { // DOM3 = IE5, NS6
          document.getElementById(id).style.display = 'none';
     }
     else {
          if (document.layers) { // Netscape 4
               document.id.display = 'none';
          }
          else { // IE 4
               document.all.id.style.display = 'none';
          }
     }
}

function showdiv(id) {
for (var i=0;i<ids.length;i++){
         divname=ids[i].id;
        if(id!=divname) {document.getElementById(divname + 'tab').className = 'notselected';  
                                                         document.getElementById(divname + 'btab').className = 'notselected';}
          else {document.getElementById(divname + 'tab').className = 'selected';
                            document.getElementById(divname + 'btab').className = 'selected'; }
     }  
     //safe function to show an element with a specified id
     if (document.getElementById) { // DOM3 = IE5, NS6
          document.getElementById(id).style.display = 'block';
     }
     else {
          if (document.layers) { // Netscape 4
               document.id.display = 'block';
          }
          else { // IE 4
               document.all.id.style.display = 'block';
          }
     }
}    

window.onload = function() {
/* this function checks for url#anchor and if found
   shows the right tab navigation content */
if (document.getElementById('cbody1') != null) {
  divId = location.hash;
  if (divId) switchid(divId.substring(1))
      else switchid("cbody1") ;
      }
}
</script>
</head>

<body><a name="top" id="top"></a>
<div id="header">&nbsp;</div>
<div id="content">
<h1>heading</h1>
  <ul>
    <li id="cbody0tab" class="notselected"><a href="javascript:switchid('cbody1');">Tab 0</a></li>
    <li id="cbody1tab" class="notselected"><a href="javascript:switchid('cbody1');">Tab 1</a></li>
  </ul>

<div id="cbody1">body1</div>

  <ul>
    <li id="cbody2tab" class="notselected"><a href="javascript:switchid('cbody2');">Tab 2</a></li>
  </ul>

<div id="cbody2">body2</div>

  <ul>
    <li id="cbody3tab" class="notselected"><a href="javascript:switchid('cbody3');">Tab 3</a></li>
  </ul>

<div id="cbody3">body3</div>

  <ul>
    <li id="cbody4tab" class="notselected"><a href="javascript:switchid('cbody4');">Tab 4 </a></li>
  </ul>

<div id="cbody4">body4</div>
  <ul id="bmenu">
    <li id="cbody0btab" Class="notselected"><a href="#top" onclick="switchid('cbody1');">Tab 0</a></li>
    <li id="cbody1btab" class="notselected"><a href="#top" onclick="switchid('cbody1');">Tab 1</a></li>
      <li id="cbody2btab" class="notselected"><a href="#top" onclick="switchid('cbody2');">Tab 2</a></li>
      <li id="cbody3btab" class="notselected"><a href="#top" onclick="switchid('cbody3');">Tab 3</a></li>
      <li id="cbody4btab" class="notselected"><a href="#top" onclick="switchid('cbody4');">Tab 4</a></li>
      <li id="theprice">&nbsp;&nbsp;blah</li>
  </ul>
</div>
</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros