Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can some one help me in debugging my js code?

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
Hi,

The following is a piece of js code that I have simplified tremendously from my company's page. It is a stand alone html file, except you don't have those gif files - sorry for that.

Here is code:

Code:
<html>
<head>
<title>LextraNet Gateway</title>
<link rel="stylesheet" type="text/css" href="/styles/0_default.css" />
<script language="JavaScript" type="text/javascript">
var isNav4, isNav6, isIE4;
function showMenu( divNum, ignore_img ) {
  if (getIdProperty( "s" + divNum, "display") == 'fubar')
    return false;
  if (getIdProperty( "s" + divNum, "display") != "block" ) {
    setIdProperty("s" + divNum, "display", "block");
    if (ignore_img != 1) document.images["i" + divNum].src = "./images/menu_tee_minus.gif";
  }
  else {
    setIdProperty("s" + divNum, "display", "none");
    if (ignore_img != 1) document.images["i" + divNum].src = "./images/menu_tee_plus.gif";
  }
}

function getIdProperty( id, property ) {
  if (isNav6) {
    var styleObject = document.getElementById( id );
    if (styleObject != null) {
      styleObject = styleObject.style;
      if (styleObject[property]) 
        return styleObject[ property ];
    }
    styleObject = getStyleBySelector( "#" + id );
    return (styleObject != null) ? styleObject[property] : null;
  }
  else if (isNav4)
    return document[id][property];
  else { 
    if (document.all[id]) return document.all[id].style[property];
    else 
      return 'fubar';
  }
}

function setIdProperty( id, property, value ) {
  if (isNav6) {
    var styleObject = document.getElementById( id );
    if (styleObject != null) {
      styleObject = styleObject.style;
      styleObject[ property ] = value;
    }
  }
  else if (isNav4) 
    document[id][property] = value;
  else if (isIE4) 
    document.all[id].style[property] = value;
}

function getStyleBySelector( selector ) {
  if (!isNav6) 
    return null;
  var sheetList = document.styleSheets;
  var ruleList;
  var i, j;
  /* look through stylesheets in reverse order that they appear in the document */
  for (i=sheetList.length-1; i >= 0; i--) {
    ruleList = sheetList[i].cssRules;
    for (j=0; j<ruleList.length; j++) {
      if (ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == selector) 
        return ruleList[j].style;
    }
  }
  return null;
}

function setBrowser() {
  if (navigator.appVersion.charAt(0) == "4") {
    if (navigator.appName.indexOf("Explorer") >= 0) 
      isIE4 = true;
    else 
      isNav4 = true;
  }
  else if (navigator.appVersion.charAt(0) > "4") 
    isNav6 = true;
}
</script>
</head>
<body onload="setBrowser()";>
<div class="mhead">
  <a onclick="showMenu(80);return false" >
    +<img name="i80" align="middle" border="0" src="./images/menu_tee_plus.gif" />
  </a>
  <img align="middle" border="0" src="./images/menu_folder_closed.gif" /> 
  <a name="row80" id="notbold" >Drafts </a>
</div>
<DIV class='subMenu' id='s80'>
<div class="empty">
  <img align="middle" border="0" src="./images/menu_bar.gif" />
  <img align="middle" border="0" src="./images/menu_tee.gif" />
  <img align="middle" border="0" src="./images/menu_link_txt.gif" /> 
  <a name="row81" id="row81"> Show All Drafts </a>
</div>
</body>
</html>

What it does:

Once you load this page (ok to open it using 'file://'), you'll see a '+' on the upper left corner, which is clickable and so is the gif next to it.

If you keep clicking it, you'll see a tree sturcture changes between collapsed and expanded.

However, if you reload this page, the tree is ALWAYS expanded.

What I want:

Every time I reload this page, the tree is in a collapsed state. But I don't know to do it.

Thank you very much for your help!!
 
I would guess that I shoudl do something inside setBrowser(). But I don't know exactly how.

Can someone help, please ?
 
I fixed it.

function setBrowser(divNum, ignore_img) {
if (navigator.appVersion.charAt(0) == "4") {
if (navigator.appName.indexOf("Explorer") >= 0)
isIE4 = true;
else
isNav4 = true;
}
else if (navigator.appVersion.charAt(0) > "4")
isNav6 = true;
//==========================
setIdProperty("s" + divNum, "display", "block");
var property = getIdProperty( "s" + divNum, "display");

if (getIdProperty( "s" + divNum, "display") == 'fubar')
return false;
if (getIdProperty( "s" + divNum, "display") != "block" ) {
setIdProperty("s" + divNum, "display", "block");
if (ignore_img != 1) document.images["i" + divNum].src = "./images/menu_tee_minus.gif";
}
else {
if (ignore_img != 1) document.images["i" + divNum].src = "./images/menu_tee_plus.gif";
setIdProperty("s" + divNum, "display", "none");
}
}

I am sure the code can be polished. Any comments/advices are still appreciated.
 
A cleaner way to do it:

Code:
<body onload="setBrowser(80);setIdProperty('s80', 'display', 'none')";>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top