Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Javascript Naming No No

Rated 3.89 (Ratings: 0)

Want more?

  • More articles in Code
 
Picture of Jeff Howden

Jeff Howden

Member info

User since: 14 Dec 1998

Articles written: 21

When naming objects on a page, most commonly images and form elements, NEVER name any object with the same name.

For instance, let's imagine you had a rather simple page like this:

<HTML>

<HEAD>

<TITLE>A simple page</TITLE>

<SCRIPT LANGUAGE="JavaScript">

<! --

function checkValue()

{

alert('The form value is ' + document.USER_INFO.USER_ID.value);

}

// -->

</SCRIPT>

</HEAD><BR><BODY>

<FORM NAME="USER_INFO">

<TABLE>

<TR>

<TD>

<IMG NAME="USER_ID" SRC="images/required.gif"

HEIGHT="20" WIDTH="20" BORDER="0" ALT="Required">

</TD>

<TD>

<INPUT TYPE="TEXT" NAME="USER_ID" SIZE="10"

VALUE="" onBlur="checkValue()">

</TD>

</TR>

</TABLE>

</FORM>

</BODY>

</HTML>

Notice that both the image and the form input element share the same name. What will happen in some browsers when the onBlur() function is called, the browser will give a JavaScript error saying that

document.USER_INFO.USER_ID is undefined

or a similar error. The browser thinks it's trying to find the value of the image object named USER_ID, which it can't do because image objects do not have a VALUE attribute associated with them.

The same would be true if you were trying to access an image object with the same name as a FORM element that it followed. It can't find the image because it occurs right after a FORM element with the same name. Since .src is not valid for a FORM element, it returns an error.

So, in conclusion, remember that if you have elements that need to have the same or similar names for functionality to not make them share names or your perfectly useful JavaScript functions will break horribly for some, if not all of your users.

Jeff Howden (.jeff) is a web developer working for Vos & Howden, LLC in Portland, Oregon where he's partnered with long-time colleague, Anthony Vos. His skills include ColdFusion, JavaScript, CSS, XML, relational databases, and much, much more. His biggest professional accomplishments include, but are not limited to:

  • building a ColdFusion-based e-commerce solution for Mt. Bachelor that transacted over $1.62 million dollars in September 2001 with 0 (yes, that's zero) ColdFusion errors and then an almost completely rebuilt version transacted $2.86 million dollars in September 2002.
  • being asked to be a Technical Editor for the ColdFusion MX book, Inside ColdFusion MX from New Rider's Publishing company.
  • being asked by BrainBench to perform quality control on their JavaScript 1.5 certification test after receiving the highest beta test score out of 200 testees.
  • managing the server that hosts evolt.org and withstanding a slashdotting that brought over 1,000,000 hits to the site, over 10 gigs of data transfer, and an average in excess of 2300 unique visitor sessions per hour, all within a 24-hour period and the server never hiccuping once.

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.