0

I created menu (tree) on my site. When user click on that menu, it will shows two more more menus. It worked on all browser except firefox browser. Can anyone tell me what is the problem?

html code

<li><a href="JavaScript:ok('tree1');">Sport</a></li>
<div id="tree1" style="display:none;">
    <li><a href="national_sport.php">National Sport</a></li>
    <li><a href="international_sport.php">International Sport</a></li>
</div>

javascript code

function ok(id){
    if(document.all(id).style.display == "none") {
        document.all(id).style.display="block";
    }
    else{
        document.all(id).style.display="none";
    }
}

Thank in advance!

Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
Sovat
  • 59
  • 1
  • 8
  • possible duplicate of [document.all vs. document.getElementById](http://stackoverflow.com/questions/2408424/document-all-vs-document-getelementbyid) – mplungjan Aug 06 '13 at 07:04

4 Answers4

4

document.all is an IE4-ism. It is non-standard and replaced about 15 years ago by (the widely supported) document.getElementById('id_of_element').

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • There's a question on stackoverflow that talks about this: http://stackoverflow.com/questions/2408424/document-all-vs-document-getelementbyid – franzlorenzon Aug 06 '13 at 07:03
1

There are quite a few issues.

  • As already mentioned, document.all is IE4+ specific code no longer needed
  • Please do NOT use the javascript protocol - there are many reasons not to and none in its favour except one: the status will show the user that the link is javascript
  • it is recommended to use unobtrusive scripting and attach the event handlers to the links in the script instead of inline.
  • your HTML is not valid

So converting a comment I made to code and adding it unobtrusively I get

Live Demo

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload=function() {
  var links = document.getElementsByTagName("a");
  for (var i=0, n=links.length;i<n;i++) {
    if (links[i].id.indexOf("_link") !=-1) {
      links[i].onclick=function() {
        var id=this.id.split("_")[0]; // get the prefix
        var sub = document.getElementById(id);
        sub.style.display=sub.style.display=="block"?"none":"block";
        return false; // stop following the link
      }
    }
  }
}
</script>
</head>
<body>
<ul>
  <li><a id="tree1_link" href="#">Sport</a>
    <ul id="tree1" style="display:none">
      <li><a href="national_sport.php">National Sport</a></li>
      <li><a href="international_sport.php">International Sport</a></li>
    </ul>
  </li>
</ul>
</body>
</html>
animuson
  • 53,861
  • 28
  • 137
  • 147
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0
function ok(id){
    if(document.getElementById(id).style.display == "none" ) {
        document.getElementById(id).style.display="block";
    }
    else{
        document.getElementById(id).style.display="none";
    } 
}
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
HMR
  • 37,593
  • 24
  • 91
  • 160
  • 1
    `document.getElementById(id).style.display = document.getElementById(id).style.display =="block"?"none":"block"` – mplungjan Aug 06 '13 at 07:05
0
function ok(id){     
var div=document.getElementById(id)
div.style.display= (div.style.display=="block")? "none" : "block";
}