Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.straub-nv.de!noris.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1398758.AFRqVoOGUt@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 05 Apr 2012 22:49:58 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: setAttribute('type') = 'button' Newsgroups: comp.lang.javascript References: <4f7ddff0$0$6869$e4fe514c@news2.news.xs4all.nl> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 106 NNTP-Posting-Date: 05 Apr 2012 22:50:05 CEST NNTP-Posting-Host: 55f9c34b.newsspool4.arcor-online.net X-Trace: DXC=m5C>;54=]gG2:OR3:3gaE@4IUKK@6JDa4d]ka`E6G^bgBc5
David Mark (8 nov 2011) It is more useful to provide the Message-ID of the not so recent posting you are referring to. For example, David might have posted several messages on that day. Also, in an international newsgroup such as this it appears to be prudent to use an internationally recognized date format, in particular ISO 8601 (that would be "2011-11-08" here) and no language-specific abbreviations of any kind there. > wrote about setAttribute and argued that you should avoid using it > (correct me if I've misunderstood). You understood correctly if David referred to implementations of W3C DOM Level 2 Core in an HTML context. Some of them are buggy there. > He also pointed out that you can't trust el['type']='button' in some > browsers. Correct. In particular, there is a bug in WebCore (the DOM implementation in WebKit, which is used in browsers by Apple and Google) with regard to objects implementing the HTMLButtonElement interface: var button = document.createElement("button"); /* "submit" */ button.type button.type = "button"; /* "submit" */ button.type Unfortunately, I could reproduce that again in Chromium 17.0.963.83 (Developer Build 127885 Linux), which is the version currently in Debian unstable, and in Chrome 18.0.1025.142 beta for Linux. > I parse xml received from the server. It may contain an element like >
> The attributes are handled via > function processXmlNodeAttribute(htmlEl,attr) { > var attrName = attr.nodeName; > var attrValue = attr.nodeValue; > htmlEl[attrName] = attrValue; > } > (There is more code to handle style elements, but this is the core.) > > This seems to work in all browsers and for all attributes - except of > course when htmlEl is an element in combination with > attr.nodeName='type'. One wonders why you would want to do that in the first place. If you have XML nodes that are HTML elements, simply import them into the document. Or, if you have CDATA, assign it to the `innerHTML' property directly or parse it. AFAICS, in neither case you need an object implementing the Attribute interface, as for every (standard) (X)HTML attribute there is a short-hand attribute property. ISTM you are asking the wrong question. > My question is: how should I efficiently test for > that condition? TIA First of all, you need to differentiate between XML, XHTML and HTML. Second, in DOM Level 2 HTML (which applies to XHTML 1.0 as well), you can assign to the `type' property of objects implementing the HTMLInputElement interface. This might throw an exception (in particular, in the MSHTML DOM), which you can catch¹. If the value of the `type' property has not changed in the meantime, you can feature-test the setAttribute() method and if that test is successful, you can call it. try { button.type = "button"; } catch (e) {} if (button.type != "button") { if (jsx.object.isHostMethod(button, "setAttribute")) { button.setAttribute("type", "button"); /* "button" */ button.type } } See also PointedEars ___________ ¹ According to my research, `try'…`catch'…`finally' can be considered a safe feature by now; i. e., if you can ignore JavaScript < 1.5 (Netscape <= 4.x). -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs,