Home Programming javaScript JavaScript best practise

JavaScript best practise

E-mail Print
Share/Save/Bookmark
Some recommendations on how to code JavaScript.

Don't use document.all

Microsoft had created its own way of referencing element objects before W3C DOM standard came into existence. It means that document.all is IE specified! W3C DOM standards are implemented in IE5 and later. So always use W3C DOM standard for referencing element objects by their id attributes.

document.getElementById("myelement");
//to reference a property
document.getElementById("myelement").name;
document.getElementById("myelement").value;

The <script> tag - don't use language attribute

<script type=”text/javascript”>
//script statements here
</script>
Don't use the old language attribute since it was never part of HTML 4.0 specification.
<script language=”JavaScript1.2”>
//don't use language attribute !!!
</script>

The <script> tag - hide JavaScript from non-JavaScript browsers

Only browsers that include JavaScript in them know to interpret the lines of code between the <script>...</script>. All other browsers (old or simplified ones) show javaScript code as a text. You can use a trick and hide code using comment symbols.

<script type=”text/javascript”>
<!--
//script statements here ...
// -->
</script>

The two forward slashes are a JavaScript comment symbol. This symbol is necessary because JavaScript otherwise tries to interpret the components of the ending HTML symbol (-->). Forward slashes tell JavaScript to skip the line entirely and a "non-scriptable" browser simply treats those slash characters as part of the entire HTML comment to be ignored.

sdsdsd

Hits: 4593
Comments (0)Add Comment

Write comment

busy
Last Updated ( Saturday, 24 January 2009 20:34 )  

Sponsored Links

My friends

Bookingpoint
partner websites

Donate

Do you find content useful? Please donate so I can cover my hosting expenses! Thanks!