Do <script tags have to appear in <head section?

D

David Mark

[snip]
setAttribute is buggy in IE, set the property directly:

It isn't buggy, but broken as designed.
js.type="text/javascript";

This is the same as using setAttribute in IE, which is the whole
problem with IE's implementation. In this particular case it doesn't
make any difference as the element property is a string and the
attribute name is not a reserved word or camel-case.

On the other hand, these will all fail in IE:

el.setAttribute('disabled', 'disabled'); // disabled property is
boolean

el.setAttribute('style', 'color:red'); // style property is an object

el.setAttribute('onclick', 'alert("test");'); // onclick property
needs a function

el.setAttribute('for', 'myID'); // for is reserved, property is
htmlFor

el.setAttribute('maxlength', '10'); // property is maxLength

The getAttribute method has the same botched design. Of course, it is
rarely useful to call getAttribute to retrieve something that is
exposed by a standard DOM property in the first place.

The feature test for this is trivial, but a full workaround is not as
IE does weird things when setting the type property of input
elements. I got tired of the get/setAttribute taboo a while back and
worked out wrappers, but they are not pretty.

It is beyond belief that MS didn't change this in IE7.
 
D

David Mark

Is there a place that setting the property directly doesn't work?

If the corresponding DOM property has problems (like the type property
of input elements), then a working setAttribute (or ugly workaround)
is a necessity.

I only use setAttribute with created elements and I virtually never
use getAttribute. I am sure you know the difference between setting
an attribute and setting a DOM property and that in most scripts the
difference doesn't matter. I have heard that it can make more of a
difference in XHTML implementations, but can't confirm that.

I have seen "arguments" posted that claim that using get/setAttribute
is "more standard" than using element properties. Obviously one is
not a replacement for the other. Regardless, there are those who
don't understand the difference and write script like:

document.getElementById('myimg').setAttribute('src', 'myimage.gif');

Certainly, using setAttribute in this way is useless. It would be
even if IE implemented it properly. And agreed that it is a dangerous
habit to get into considering IE's pitfalls.
 
D

David Mark

David Mark said the following on 10/11/2007 9:27 PM:



That would explain why I have never had a need to use it. I have never
tried changing the type of an input element. If I wanted a different
type, I would simply remove the existing and replace it.

The only problem (or at least one that I remember) is that created
radio inputs have problems. This may have something to do with a bug
in IE concerning setting the name property of a new input element. I
can't remember the exact details, but unless you set the name property
and type property and append the element in a certain order, you get
either an unusable radio input or a text input. Once I got my wrapper
to work with radio inputs, the name problem went away (ie inputs could
be found by name in the elements collection and actually showed up on
the server when the form was submitted.) It makes sense that the two
issues are related as radio inputs without names are useless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top