On Aug 23, 2:48 am, David Golightly <davig...@gmail.com> wrote:
> On Aug 21, 11:15 pm, Ding <xdd...@gmail.com> wrote:
>
> > -----------------------------------------------
> > is there something like this:
> > ie: element.setAttribute("className", "myCss");
> > firefox: element.setAttribute("class", "myCss");
>
> IE chokes on setting the "name" attribute of an element that's already
> in the DOM. See:http://msdn.microsoft.com/library/de...shop/author/dh...
> for MSDN's article on the issue. Unfortunately, the only way to
> successfully do this is nonstandard and extremely bad practice. Seehttp://www.easy-reader.net/archives/2005/09/02/death-to-bad-dom-imple...
> for an example of what you're up against.
I had a play with this, it seems that every solution uses either
browser sniffing or try..catch with IE's version of createElement
first then the W3C version second.
As an alternative, I thought about adding an element with a name using
W3C createElement, then trying to get it back using
getElementsByTagName. If it works, use a W3C createElement method.
If it fails, use an innerHTML based method for creating elements
(rather than assuming IE and using its version of createElement, the
string that needs to be constructed is the same).
A simple test is here, based on the idea that the head element is
always available to scripts, and that a meta element can have a name
attribute and can be added to the head:
function checkName() {
var randomName = 'foo_' + (new Date()).getTime();
var newElement = document.createElement('meta');
newElement.name = randomName;
document.getElementsByTagName('head')[0].appendChild(newElement);
return !!(document.getElementsByName(randomName)[0])
}
alert("Use W3C createElement? " + checkName());
It works in Firefox and IE 6 (all I have available at the moment).
It's just a proof of concept and needs a lot more effort yet, but do
you think it's a suitable feature test to use with a generic element
builder function? It could be delayed using window.onload, but I
think it should run as soon as possible.
Incidentally, the authors of Prototype.js say they have fixed the IE
name attribute issue with their new element builder functions, but
they rely on browser sniffing (a reliance that seems to be an
increasing in a number of libraries - v 1.6.0_rc0, #1579).
--
Rob