[web] Why won't this this javascript work in Firefox/Netscape?

Started by
5 comments, last by markr 19 years, 2 months ago
Hi I've got this &#106avascript, and I can't get it working in either Firefox or Netscape, but it works fine in IE. It is really strange: <html> <head> <script language="&#106avascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input name="andrew" type="text" /&gt; &lt;input name="but" type="button" &#111;nclick="andrew.value='d'" /&gt; &lt;/body&gt; &lt;/html&gt; I can't work out what the problem is. I thought something as simple as this should work. Thanks
Advertisement
Maybe because the 'id' property of the input button hasn't been set as well?

Just a guess here, I don't have wither Firefox or Netscape installed on this particular machine.
its because its not entirely valid, IE usually will interpret what you mean and work anyway while the other browsers are more rigid try this


<html>
<head>
<script language="&#106avascript"></script>
</head>
<body>
<input id="andrew" name="andrew" type="text" />
<input name="but" type="button" onclick="andrew.value='d';" />
</body>
</html>

this control should be referenced using the id property. Hope this helps

EDIT : beaten and yes i've tested this and it does need the id prop.
Go Aussies!
Do not assume that any named item automatically has a property of the same name existing on the "window" object.

This is a "feature" (i.e. misfeature, bug, annoyance, brokenness) of IE.

No other browser does it or has ever done it (mostly).

If you want to find an object in the page, use the correct method. Generally speaking, the easiest way is getElementById(), but you can also use document.images, document.forms, etc for images and form elements.

So it might read:
<input name="andrew" id="andrew" type="text" /><input type="button" onclick="document.getElementById('andrew').value = 'd'" />


Mark
Ah I see, thanks alot.

Also I didn't realise you were supposed to create a form to get access to the objects aswell.


That 'feature' i guess is to stuff up competing browsers.
No, it's not to "stuff up" competing browsers. It's because when MS made IE4, they went a bit nuts and decided to make everything as VB-ish as possible "hey we don't need no DOM, let's just add *everything* to the window object".

Then they realised that this was daft, and added DOM features, but didn't remove the "add everything to window" feature.

The reason that "add everything to window" is bad is because of name clashes. What happens if you make an id of an element "navigator"? Well, it doesn't get added to "window" because there's already a built-in object there. What about the more obscure "ActiveXObject" ? Well, there's one of those, but it's the constructor function for ActiveX objects.

As such it gets rather silly to just plonk everything into the global namespace. It causes problems unless you're aware of it when you're coding (assuming you're coding for a sensible browser, like Mozilla).

Oh yes, did I mention that IE's &#106avascript errors are pretty useless too? And that its debugger sucks (Hey, at least it has one, unlike Safari's "&#106avascript errors? what &#106avascript errors?" approach)<br><br>Venkman is your friend.<br><br>Mark

This topic is closed to new replies.

Advertisement