<html> <head> <title>Javascript Page</title> <script language="JavaScript" type="text/javascript"><!-- function init(){ alert("Your browser supports scripting!"); } --></script> </head> <body onload="javascript: init();"> <h1>Javascript Page</h1> <noscript>This will print if the user agent does not support javascript</noscript> <p>This will always print.</p> <script language="JavaScript" type="text/javascript"><!-- document.write("You can also run scripts within the body of the page."); --></script> </body> </html>
function getElem(elemID){ if(document.all){ return document.all(elemID); }else if(document.getElementById){ return document.getElementById(elemID); }else if(document.layers){ return document.layers[elemID]; } } ...before doing anything with the object, you can make sure you have a valid object like this: var elem = getElem("SomeElementId"); if(elem){ // elem is valid, so proceed with code }
<SCRIPT language="JavaScript"> <!-- This opens the HTML comments that will hide the script from old browsers // javascript comment ......Javascript Statements....... /* multi-line javascript comment */ //--> This closes the comment section and the browser will read on normally </SCRIPT>
// in the <head> section of an html page <script type="JavaScript" src="scriptFilename.js"></script>
if(elem && typeof elem.property != "undefined"){ // element property is valid - it is safe to use this property } if(elem && typeof elem.method != "undefined"){ // element method is valid - it is safe to call this method }