Table of Contents

JavaScript Notes

Baseline Example

<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>

Getting a reference for an element in any browser

Commenting

<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>

Linking to an external script file

// in the <head> section of an html page
<script type="JavaScript" src="scriptFilename.js"></script>

Testing for existence of a property or method

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
}