Because JavaScript must ignore comments, there is an appropriate syntax for demarcating text as a comment. For single line comments, simply precede the line with two slashes //. For multi-line comment blocks, begin the comment with /* and close with */.
At NetMechanic they use a method that works in all browsers that have cookies turned on. Just insert this JavaScript code in the HEAD section of your document:
<script>
<!--
// give feedback to the respondent about the state of their submission
function AllowNoDups()
{
var cookie_ls = document.cookie;
if (cookie_ls.indexOf(document.location) > -1)
{
alert("You've already submitted your answers. Thank you for your interest! ");
return false;
}
else
{
document.cookie = window.location.href + " from " + document.referrer + "; path=/; expires=Thu, 23-Aug-2012 00:00:01 GMT;";
return true;
};
};
//-->
</script>
-->
You'll call the function inside the opening FORM tag like this:
<FORM onSubmit="return AllowNoDups();">
When a visitor submits the form, the function sets an identifying cookie. Before the form processes, the function checks to see if there is already a cookie set. If not, the form handles the submission as normal. If there is a cookie, the visitor gets an alert box that says "You've already submitted your answers" and the form submission is blocked. This is a good, cross-browser way to avoid duplicate submissions, but be aware that the visitor's browser must accept cookies and have JavaScript turned on for this method to work.
Take a look at the footer of this page (view the frame source). There is a JavaScript that gives the "last updated" value, based on when the page was saved. It's simple, and is much easier than trying to remember to do it manually every time you update a page. Here's the code:
<script language="JavaScript" type="text/javascript">
<!--
document.write("This page was last updated " + document.lastModified);
// -->
</script>
With HTML 4.0, in order to avoid the CDATA enclosure requirements for scripts and stylesheets, an external JavaScript script file is recommended. SPAUG uses one named spaug_util.js that contains the code for the above:
function updated () {document.write("This page was last updated " + document.lastModified);}
which, when used in conjunction with the call to the script:
<script language="JavaScript" type="text/javascript">updated ()</script>
will write the last saved date of the document.
<script language="JavaScript">
<!-- FRAME BREAKER
<!-- IT WILL BREAK ANY PAGE OUT OF FRAMES
if (window != window.top)
top.location.href = location.href;
// -->
</script>
<script type="text/javascript">
if (document.images)
{
image1 = new Image;
image2 = new Image;
image1.src = "enter1.gif";
image2.src = "enter2.gif";
}
function chgImg(name, image)
{
if (document.images)
{
document[name].src = eval(image+".src");
}
}
</script>
...
<a href="/" onMouseOver='chgImg("enter", "image2")'
onMouseOut='chgImg("enter", "image1")'><img name="enter"
src="enter1.gif" border="0" alt="Enter if you dare!"></a>
A JavaScript bug in recent versions (5 and 6) of Internet Explorer may cause problems with JavaScript rollovers. When you use an absolute image path link inside your JavaScript rollover code, these versions of Explorer ignore the cached copy and request the file from the browser each time it's needed.
This problem only happens when you use an absolute image path to refer to the rollover image. If you use relative links for your rollover images, Explorer checks the cache and retrieves them without a problem.
Here is an example on the WebSIG site of three different mouseOver effects, and info about detecting browser versions, and hiding JavaScript from older browsers.
<script language="JavaScript1.2" type="text/JavaScript">
<!-- Hide script from older browsers
function OpenWindow (URL, WinName, Features) { //v2.0
window.open(URL, WinName, Features);
}
// End hiding script from older browsers -->
</script>
Use it in the body to open a web page in a new window using the parameters, for example:
<a href="javascript:OpenWindow('dailynews.html','ltwin','width=600, height=500, left=20, top=20, screenX=20, screenY=20,
outerwidth=604, outerheight=504, resizable, scrollbars')">Click Here For Breaking News</a>
For another version, see www.netmechanic.com/news/vol4/javascript_no7.htm.
Sometimes you'll need to have JavaScript events and event handlers within HTML tags, in the body of your page. An event is a way of identifying certain actions that happen in the browser. An event handler is a way of performing an action when a specified event occurs.
The most common application of this is mouseovers. The JavaScript you use within the HMTL anchor tag generates a value that is passed back to another script in the file which controls the graphics or messages that appear when the reader mouses over an item on your page.
For example, when you move your mouse over the menu options at the side of this section you'll see that the little bar changes from red to purple as you move the mouse. This is controlled with a script. In the body of the file, at each link, there is an eventhandler that watches for the mouse to move atop a menu item. The JavaScript elements which are inserted into the anchor tag look like this: <a href="home.html" onMouseOver="msover(0); return true" onMouseOut="msout(0); return true">
Remember, in most cases where you use JavaScript elements within an HTML tag, you have another script elsewhere in your page, defining the variables, arrays or other required elements. For example, in the mouseover example above, there is another script earlier in the page that defines the placement of the red and purple lines based on the state of the mouseover events.
Sometimes you'll use the anchor tag to call a JavaScript function. Typically, you'll do this when you want to perform some action when the reader clicks on a link.
In order to use a JavaScript function in an anchor tag, it must either be a standard, pre-defined function or a function that you created earlier in the page. The function you create will be within the script tags in the head of your file. Once it is created, you can call it from the standard HTML anchor tag.
For example, you may have a script that opens a new window of a certain size when the reader clicks on a linked word. The new window might contain a definition of that word.
<a href='javascript:openAWindow("example.html",150,150,1)'>green iguna</a>
When the reader clicks on the linked words green iguna, the anchor tag looks for a JavaScript function named openAwindow, and opens a window that is 150 by 150 pixels in size.
Basically, you are just substituting the phrase "javascript:", the name of a function, and that function's attributes for the URL that you would normally call.
Consider using JavaScript to parse the email address components to hide them from email spiders. Be sure to change the variable names to reflect your site and email address! Also, remember to keep the document.write statement on one line in your HTML code to avoid getting a JavaScript error message on your Web page.
Place the script in the BODY section of your document.
<p>Send your comments and questions to our
<script language="JavaScript" type="text/javascript">
<!--
var contact = "Webmaster"
var email = "stan.hutchings"
var emailHost = "gmail@#46;com"
document.write("<a href=" + "mail" + "to:" + email + "@#64;" + emailHost+ ">"+ contact + "</a>" + "@#46;")
//-->
</script>
</p>
Looks like this:
Send your comments and questions to our
This works great if you only have a couple of email addresses on your page. A long list of contacts is harder to easily manage and maintain.
A more minimalist version follows (yes, you need all four lines; replace emailID with the part of the email in front of the @ sign, and host.domain with the part after the @ sign; the code @ inserts the @ sign in the mail and . inserts the . [dot]:
<script language="JavaScript" type="text/javascript">
<!--
document.write("<a href="+"mail"+"to:"+"emailID"+"@"+"host.domain> email Name</"+"a>");
// --> </script>
and will render like this:
External JavaScript files are a great way to use the same JavaScript functions on every page and reduce space and maintenance problems. The URL and last updated message at the bottom of most SPAUG pages is an example of this in action.
You may think external JavaScript files hide the source code, because the code doesn't display with the View Source option. But the script file can be opened as a text file from the browser cache - look for a .js extension on a recent file, and open it with NotePad. So if you see a JavaScript that is really cool, you can examine and dissect it, and perhaps use it on your own site.
Remember to validate your JavaScript code before you put it into the external file. That means include it in the HEAD and BODY as required. Once the code works, create your external file like this:
This technique was provided by AtomZ for their search form. You can easily place the focus of your cursor in your search form by default. This will enable your visitors to be able to type their query without clicking in the search field first. To add this functionality to a Web page, follow the two steps below.
1. Copy and paste this code into the HEAD section of the page.
<SCRIPT LANGUAGE= "Javascript">
<!--
function placeFocus(){
document.forms[0].elements['sp-q'].focus(); }
// End -->
</SCRIPT>
This code is written to look for the sp-q value in your search form. If you're using something other than sp_q, you'll need to modify the JavaScript to match. Also note that this code specifies form.document(0). That indicates the first form on the page. If you have other forms on your page, you'll need to specify the correct number for your search form.
2. Add the following OnLoad command into your BODY tag.
<BODY OnLoad="placeFocus()">
When you set this up on your Web pages, you might also want to do the same in your template for your search results page. To modify your search results template, follow the Templates link to "Template Editor" and do the following:
1. Paste the JavaScript code from Step 1 above into the HEAD section.
2. Find the <SEARCH-BODY> tag in your template and add the OnLoad command to it, like this:
<SEARCH-BODY OnLoad="placeFocus()">
<script SRC="ssm.js" language="JavaScript1.2">
// example from www.leavingcal.com/ ; need ssm.js JavaScript file, or extract applicable functions
//Dynamic-FX slide in menu v6.5 (By maXimus, maximus@nsimail.com)
//Menu Bar slides down page as it is scrolled, so is alway in view, and opens to show Menu items only on MouseOver
//Site: www.absolutegb.com/maximus
//For full source, and 100's more DHTML scripts, visit www.dynamicdrive.com
</script>
<layer name="basessm1" top="150" left="0" visibility="show">
<ilayer name="basessm2">
<layer visibility="hide" name="thessm" bgcolor="black" left="-150" onmouseover="moveOut()" onmouseout="moveBack()">
<table border="0" cellpadding="0" cellspacing="1" width="172" bgcolor="black">
<tr><td bgcolor="#000080" height="15" align="left" valign="center" width="149" colspan="2"> <font face="verdana" size="2" color="white"><b>menu</b></font></td>
<td align="center" rowspan="100" width="20" bgcolor="#000080" valign="center"><p align="center"><font face="verdana" size="2" color="white"><b>L<br>E<br>A<br>V<br>I<br>N<br>G<br> <br>C<br>A<br>L<br> <br>M<br>E<br>N<br>U<br></b></font></p></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="index.shtml" target="_parent" class="ssmItems">Home Page</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="poll.shtml" target="_parent" class="ssmItems">Polls</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="cgi/eboard31/index2.cgi" target="_parent" class="ssmItems">Voice Your Opinion</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="bleak.shtml" target="_parent" class="ssmItems">Bleak Outlook</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="compare.shtml" target="_parent" class="ssmItems">Compare State</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="quiz/popquiz.htm" target="_parent" class="ssmItems">Quick Quiz</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="weststates.shtml" target="_parent" class="ssmItems">Western States</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="eaststates.shtml" target="_parent" class="ssmItems">Eastern States</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="74" colspan="1"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="mailto:info@leavingca.com" target="_top" class="ssmItems">Contact</div></layer></ilayer></td><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="74" colspan="1"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="privacy.html" target="_top" class="ssmItems">Privacy</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="#000080" height="15" align="left" Valign="center" width="149" colspan="2"> <font face="verdana" size="2" color="white"><b>Additional Links</b></font></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="advertise.html " target="_parent" class="ssmItems">Advertise With Us</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="relocation.shtml" target="_parent" class="ssmItems">Relocation Experts</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="send2friend.html" target="_parent" class="ssmItems">Tell A Friend</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="chatpage.html" target="_parent" class="ssmItems">Chat Room's</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="quote.html" target="_parent" class="ssmItems">Get a Quote</div></layer></ilayer></td>
</tr>
<tr><td bgcolor="white" onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="149" colspan="2"><ilayer><layer onmouseover="bgcolor='#FFFF99'" onmouseout="bgcolor='white'" width="100%" align="left"><div align="left"><font face="Verdana" size="2"> <a href="javascript:OpenWindow('dailynews.html','ltwin','width=500,height=450, left=20,top=20,screenX=20,screenY=20,outerwidth=504,outerheight=454,resizable,scrollbars')" target="_parent" class="ssmItems">Breaking News</div></layer></ilayer></td>
</tr>
</table>
</layer>
</ilayer>
</layer>
An advanced article at O'Reilly Network by Scott Andrew LePera (Aug '01). Document Object Model (DOM) provides a scriptable interface for a document structure. With the DOM, an HTML document is described as a series of nodes, each node representing an object in the document, including all text, tags, comments, and other data. Altering these nodes through JavaScript, a developer can alter the visual display of the page in a browser. Learn how to use DOM methods to create, insert, and remove elements and text nodes from the page in part I, and how to use the JavaScript node interface to alter element attributes with DOM element methods, and change an element's style properties through the DOM Level 2 (DOM2) Style specification interface in part II. Part I of II and Part II of II.
![]()