Javascript:
1) Indent nicely.
2) Reuse variables.
3) If you find yourself declaring variables in functions over and again, make them global.
4) You needn't repeat "var" all the time; use one "var" keyword and seperate var=value pairs with commas.
5) If you only have one statement in an if or else, you don't need to use braces. Doesn't apply to functions.
6) Don't use tabs. They're too big. I usually use two spaces.
7) Use arrays and for loops rather than long lists of variables where possible.
8) A matter of opinion, but I prefer the "Java" brace layout style (opening braces are always on the same line as their keywords; closing braces go on a seperate line, unless they have a related keyword directly after).
Code:
function fish() { // function definition - increase indentation on following lines
if(blather) { // keyword - increase indentation again
some_code();
} else { // end of keyword - decrease indentation on _this_ line and those following - but then a related keyword, so increase again
some_mode_code();
} // end keyword, on a seperate line. Decrease indentation here and onwards.
} // end function definition.
HTML:
1) Indent nicely.
2) Don't use double-linebreaks, unless you want to make something really stand out in the code. Loses its effect if overused.
3) HTML (tags, attributes and attribute values if a set value [like action="get" and shape="rect"]) should be all lower-case. In fact, this is a validation requirement in XHTML.
4) The "language" attribute for <script> is deprecated for "type."
5) The validator is your friend as far as content's concerned, though it won't help with code layout.
Your code, neatened and pruned:
Code:
<html>
<head>
<title>Menu Test</title>
<link rel="stylesheet" href="global.css" type="text/css"/>
<script type="text/javascript">
firsttime = true; // avoid defining variables twice.
// we can't have the variables defined until the page loads, because they need their respective elements.
searchforms = new Array();
function init() {
firsttime = false;
searchforms[0] = document.getElementById("wholeformcontainer");
searchforms[1] = document.getElementById("maintestarea");
searchforms[2] = document.getElementById("shadowlayer");
searchforms[3] = document.getElementById("shadowlayer2");
}
function hidesearchform() {
if(firsttime) init(); // put it here, to avoid messing up your onload.
for(var i=0;i<searchforms.length;i++)
searchforms[i].style.visibility = "visible";
}
function expandsearchform () {
for(var i=0;i<searchforms.length;i++)
if(searchforms[i].style.visibility == "hidden")
searchforms[i].style.visibility = "visible";
}
</script>
</head>
<body onload="hidesearchform()">
<div id="menubutton"><a href="#" onclick="expandsearchform()">Modify Search</a></div>
<div id="wholeformcontainer">
<div id="maintestarea"></div>
<div id="shadowlayer"></div>
<div id="shadowlayer2"></div>
</div>
<div id="testworkingarea"></div>
</body>
</html>
Bookmarks