Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13247

Re: setAttribute('type') = 'button'

Message-ID <1398758.AFRqVoOGUt@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-04-05 22:49 +0200
Subject Re: setAttribute('type') = 'button'
Newsgroups comp.lang.javascript
References <4f7ddff0$0$6869$e4fe514c@news2.news.xs4all.nl>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Tom de Neef wrote:

> 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
> <form id='form3'><input id='in3' type='button' onclick='inform(event)' ...
> 
> 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 <input> 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 
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fdom%2Fwidgets.js>


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, <f806at$ail$1$8300dec7@news.demon.co.uk>

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

setAttribute('type') = 'button' "Tom de Neef" <tdeneef@qolor.nl> - 2012-04-05 20:09 +0200
  Re: setAttribute('type') = 'button' Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-05 22:49 +0200
    Re: setAttribute('type') = 'button' "Tom de Neef" <tdeneef@qolor.nl> - 2012-04-06 10:43 +0200

csiph-web