Sign in to follow this  

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

This topic is 4695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I've got this javascript, 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="javascript"></script> </head> <body> <input name="andrew" type="text" /> <input name="but" type="button" onclick="andrew.value='d'" /> </body> </html> I can't work out what the problem is. I thought something as simple as this should work. Thanks

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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="javascript"></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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 javascript errors are pretty useless too? And that its debugger sucks (Hey, at least it has one, unlike Safari's "javascript errors? what javascript errors?" approach)

Venkman is your friend.

Mark

Share this post


Link to post
Share on other sites

This topic is 4695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this