internet.com
Library
Magazines

webreference.com

Java Boutique

Search Engine Watch

PC Webopedia

The Web Developer's Virtual Library

Library

JavaScript Q&A Mailbag, Part II

by Heidi Brumbaugh

Judy's letter continues, "Another problem I had was when I clicked one of my other links on the page and pressed the back button to come back to my original page, the images for the mouseover weren't there again and I had to run my mouse over the question mark icon to get them to reappear. Any thoughts on what I did wrong?"

You didn't do anything wrong; the page is working correctly. Keep in mind that HTML files don't "remember" the state they were in the last time the user visited. They load from scratch each time. If you want to remember whether as user passed over an image, you'll need to use cookies. See Scott Clark's article for an excellent primer on doing this in JavaScript.

A few people wrote to say that the mouseover doesn't work correctly, because the image doesn't restore when they move the mouse out of the image area. This is simply because I didn't add that functionality. This article's example does demonstrate using the onMouseout event to restore an image.

Obviously this topic is of much interest to Web designers. Here are some questions on some other topics.

Aaron Prohaska wants to know, "Is there any way to write a script that would automaticaly send me an email with the browser and history of a user who visited my site. I don't completely understand how to get cookies to work but I'm not sure I need to use cookies for this." I asked Scott Clark (WebDeveloper.com’s Technical Editor) about this, and he said, "You can get the user's Web browser using JavaScript, and you may even be able to pass it as a variable which would become the email's subject...heck, you may even be able to put it in a hidden form as the body of the email message, but you cannot get a user's history. And when all is said and done, you won't be able to send the email without them knowing. You can make the form hidden, and use an onLoad action to submit the form, but the little box will still come up and let the user know that an email is being sent. Ever since Netscape 2.2, that's been a "fix" to an earlier privacy bug. So it is not possible to get the information that this reader is wanting to get, at least not without okaying it with the user."

Gene Strocco asks a great question about form verification for email fields: "Is there a way to have Javascript verify that the @ symbol was entered by the user? I know that many people do not enter valid email addresses when they fill out forms, and I realize that although JavaScript cannot verify a completely valid email address, I thought it might be able to verify that a halfway decent address was entered."

Sure, we can do that. This is just the sort of thing JavaScript loves. To check for the @ symbol, we'll use the indexOf method of the String object. indexOf returns the position of a string inside of another string, or -1 if the string we're looking for isn't found. Here's a routine that returns 1 if an @ character is found with at least one character both before and after it.

function CheckEmail(TheAddress) {
var valid = 1
var atPos = 0

   // First make sure @ is at least at position 1. 
   // Remember the first position is 0
   atPos = TheAddress.indexOf("@")
   if (atPos < 1) {
      // Return false if @ isn't found.
      valid = 0
   }
   else {
      // If @ is found, make sure it isn't last character in string.
      if (atPos == TheAddress.length-1) {
         valid = 0
      }
   }
   return valid
}

Click here to see the routine implementing the example for form verification from a previous article.

Thanks again to everyone whose email contributed to this column.


Heidi Brumbaugh has been a writer and editor in the computer publishing industry for ten years. Feel free to visit her home page.
Web Developer® Site Feedback
Web Developer®
Copyright © 2000 internet.com Corporation. All rights reserved.

Web Developer® Home Over a dozen topics in detail Live Chat Downloads Book and Product Reviews Threaded Discussions How-To/Articles/Links Developer Daily News Subscribe Search Corporate Information Advertise Events Publications internet.com Home