Forum Moderators: open

Message Too Old, No Replies

Moving to external JS causes an Error

document.form has no properties?

         

borisbaloney

3:21 am on Jan 13, 2004 (gmt 0)

10+ Year Member



As a javascript novice, I am hoping someone can help me move my javascript to one central external file.

All the JS does is go to a location via a drop-down list box. Eg.
<form>
<select NAME="site" onChange="javascript:formHandler(this)">
<option SELECTED VALUE="">Select Page...</option>
<option VALUE="http://www.example.com.au/">Home</option>
<option VALUE="http://www.example.com.au/page2.html">Page 2</option>
<option VALUE="http://www.example.com.au/page3.html">Page 3</option>
</select>
</form>

The site did have the following on every page head:
<script LANGUAGE="JavaScript">
function formHandler(form){
var URL = document.form.site.options[document.form.site.selectedIndex].value;
window.location.href = URL;
}
</SCRIPT>

This worked fine.

Now my js is linked from the page head using:
<script type="text/javascript" language="JavaScript" src="../js/javascript.js">
</script>

The error given by Mozilla javascript: command is this:

Error: document.form has no properties
Source File: file:///(location)/js/javascript.js
Line: 2

Any ideas?

Purple Martin

6:22 am on Jan 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the script wasn't changed when you cut'n'pasted it should work exactly the same in the .js file as in the page head.

BUT

A reference to the form (this) is being passed by the onChange event. You can see that where it says onChange="javascript:formHandler(this)".
It is accepted by the function with the name form. You can see that where it says function formHandler(form){.
Therefore you don't need document.form, you just need form.

borisbaloney

11:21 am on Jan 13, 2004 (gmt 0)

10+ Year Member



Thanks for the tip PM.

I've now got everything running. I needed a name for the form. Also, thanks for the help teaching me about the JS DOM.

Boris.