- Tips and Tricks -

The JavaScript Mini-FAQ

This is a list of frequently-asked questions about JavaScript and answers. This is compiled from questions I've been emailed, and from frequent questions on the comp.lang.javascript newsgroup. Please check this list before emailing me a question. (Currently, this is adapted from the mini-FAQ I post to the comp.lang.javascript newsgroup. It will eventually be updated to be more friendly and more specific to this site.)

JavaScript Basics and Pointers

What is JavaScript? Is it different from Java?

JavaScript is a simple, object-oriented scripting language which you can use directly within a web page. It was originally known as LiveScript. JavaScript is an interpreted language, and is supported on most platforms by Netscape. You don't need anything but a text or HTML editor to start using JavaScript.

Java, on the other hand, is a compiled language similar to C++. Java applets are included on a web page with the <APPLET> tag, and are self-contained. Java is, at the moment, supported only on 32-bit platforms.

There's not much similarity between Java and JavaScript; JavaScript got its name due to a licensing agreement between Netscape and Sun. The languages have different capabilities and different features, and each has its purpose - they can even communicate with each other, using the LiveConnect feature of Netscape 3.0.

What versions of JavaScript exist?

Which browsers support JavaScript?

Currently, Netscape and Microsoft Internet Explorer (MSIE) are the two browsers that support JavaScript to some extent. Until recently, JavaScript 1.0 (Netscape 2.0) was considered to be the most stable implementation. The final version of Netscape 3.0 has just been released, though, and is mostly stable.

If you use any of the 3.0-specific features - such as data tainting, dynamic images, or LiveConnect - Netscape 3.0 will be the only supported browser. MSIE is currently limited to Netscape 2.0's feature set, and there are a few incompatibilities even so.

Also bear in mind that although Netscape supports JavaScript on all platforms, there are minor differences between the platforms. Many of these have been eliminated in Netscape 3.0 (JavaScript 1.1).

Where can I find the documentation?

Follow these links: Netscape 3.0 (JavaScript 1.1) Netscape 2.0 (JavaScript 1.0) MSIE's objects and scripting

Are there other FAQs?

This isn't the only FAQ, although it's currently the only one that is regularly posted to the comp.lang.javascript newsgroup. The others are:

Browser Functions and JavaScript

Can I add a bookmark from within JavaScript?

Nope. JavaScript does not include any features to control Netscape's bookmarks, and most of us agree that this is a Good Thing. Just imagine if every site you visited added a bookmark to your menu without asking. To my knowledge, this isn't possible with any other language either.

Can I access the "print" function?

Similarly, no. There are some good reasons to want to do this - for example, including a "print" button in a window with no toolbar - but Netscape offers no such feature.

Can I disable the "Back" button? Or any other button or menu item?

Once again, nope. This is a functionality that would be abused if it existed.

One note: if you use the location.replace() method in Netscape 3.0 to change URLs, the user won't be able to use the back button because no history record is kept.

Can I make a "Back" or "Forward" button?

Yes. Use the history.go() method. For example:
   <INPUT TYPE="BUTTON" VALUE="Back" onClick="history.go(-1);">
   <INPUT TYPE="BUTTON" VALUE="Forward" onClick="history.go(1);">
You can also specify a frame name to go back or forward within that frame, such as parent.frame1.history.go(-1).

Can I use JavaScript to force a page to reload?

Yes, in two different ways. The first does a "soft" reload, which always loads from the cache:
   history.go(0);
The second method, available in Netscape 3.0 only, does a true reload, similar to the reload button:
   window.location.reload();
Adding (true) within the parentheses will force this command to reload regardless of the modification date of the file.

Can I load a different page?

Yes. Set the window.location property:
    window.location = "http://www.netscape.com/";
or  window.location.href = "http://www.netscape.com/";
(the first statement may not work consistently in MSIE)

If you're using multiple frames, specify a frame instead of the window object:

    parent.frame1.location = "http://www.netscape.com/";
With Netscape 3.0, you can also use the location.replace method. This method loads a new page, but overwrites the current history entry rather than keeping track.
    location.replace("http://www.netscape.com/");
Bear in mind that if you replace the current document using any of these methods, your script will stop executing and disappear.

Can I hide my JavaScript source code from users?

Simply put, no. There are a few tricks - such as leaving carriage returns out of your document, or using the <SCRIPT SRC> feature to include it. However, none of these will prevent anyone but the most casual user from seeing your source code.

Note that you CAN copyright your source code, even though it's viewable. Include a copyright notice on the page and in the source code. Just because people can steal it doesn't mean they're entitled to; this is similar to HTML documents.

A final tip: Add a unique identifier somewhere in the script, disguised as a cryptic function. For example:

  value1 = 8675309;
You can then periodically search Alta Vista (altavista.digital.com) for the identifier to see if anyone has stolen your script verbatim.

JavaScript Language Capabilities

Can I automatically send mail when a user loads my page?

No. This was possible in early versions of Netscape, but has been disallowed since Netscape 2.01 for security reasons. The only way to mail anything is by having the user explicitly click a SUBMIT button.

Can I read or write text files? Or read an entire HTML page?

Once again, no. JavaScript doesn't have any file or network I/O capabilities. You can load web pages into frames and read their properties - with restrictions - but you can't read the entire text of a page.

How can I hide JavaScript from older browsers?

The usual method is to use HTML comments, like this:
   <SCRIPT LANGUAGE="JavaScript">
   <!-- // (hide from old browsers)
   ... program goes here...
   // stop hiding  -->
   </SCRIPT>
If you use this method, you'll have to avoid certain characters within your script, specifically > (greater than) and -- (decrement). You can substitute for them: Also, be warned that this won't work with all browsers. There are certain brain-damaged ones, such as the AOL browser, which will display the JavaScript code anyway. The only completely effective way to do this is with the <SCRIPT SRC> tag, available in Netscape 3.0.

How can I display a message for non-JavaScript browsers?

Netscape 3.0 includes a <NOSCRIPT> tag, which you can use to enclose content that will be displayed to users of non-JavaScript browsers. Thus, you could do this:
  <NOSCRIPT> Please see the non-JavaScript
version. </NOSCRIPT>
Unfortunately, this text will also appear in Netscape 2.0. A tricky solution that works in all versions is something like this:
         <SCRIPT LANGUAGE='JavaScript'>
         <!-- //hide from old browsers
         ...program goes here...
         /* (start JavaScript comment, end HTML comment)
         -->
         You're using a non-JavaScript browser! Shame on you.
         <!-- */ // -->
         </SCRIPT>
Note: Don't attempt to use the <NOFRAMES> tag for this purpose. There is no connection between frame support and JavaScript support.

Can I make a link that performs a function instead of going to a URL?

Yes. There are two common methods. The first works most of the time, but fails occasionally on Macintosh versions of Netscape 2.0:
   <A HREF="#" onClick="function();">link</A>
The second works all of the time, but may change the current URL:
   <A HREF="javascript:function();">link</A>

Can I make a [link, button, etc] that updates two frames at once?

Yes. Just use a function to update both frames:
   function update(loc1,loc2) {
      parent.frame1.location.href = loc1;
      parent.frame2.location.href = loc2;
   }
<A HREF="javascript:update('http://www.x.com/','http://www.y.com/');">link</A>

How do I get the value of a select object?

Do something like this:
   item = document.form.selectname.selectedIndex;
   value = document.form.selectname.options[item].value;
If the selection list allows multiple selections, you'll have to check each of the choices in the options[] array individually.

Common problems and errors

Some of my event handlers don't work. Why?

This is usually caused by a lack of WIDTH and HEIGHT attributes on images. Be sure that all of your images include these attributes. Leaving these out can also cause crashes on JavaScript-generated documents.

I'm trying to access a frame's properties. It works with local documents, but with non-local ones I get an error: "Access Denied from scripts at ..."

This is a security feature added with Netscape 2.01. You can access these properties in Netscape 3.0 if you turn on data tainting; see the 3.0 version of the JavaScript Authoring Guide for details about tainting (it's also covered in my book.

When I use the <SCRIPT SRC> tag, the script is displayed and not interpreted.

This is because your server isn't set up to handle JavaScript (.js) files. You need to add the following MIME type to the server's configuration:
application/x-javascript      .js
If you don't know how to do this, contact your server's administrator.

When I use document.lastModified, I get a date in 1969. Why?

The lastModified property is buggy in some versions of Netscape 2.0, and is inconsistent in others and in MSIE. On top of that, some servers send the Last Modified: header in a strange format, or not at all.

One thing to look out for: with some servers, enabling server-side includes (SSI) causes last-modified dates to be incorrect.


If you have a correction or addition for this FAQ, please send it to jsfaq@starlingtech.com. Thanks!
michael moncur ... jsbooks@starlingtech.com