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


Groups > comp.lang.javascript > #13246 > unrolled thread

setAttribute('type') = 'button'

Started by"Tom de Neef" <tdeneef@qolor.nl>
First post2012-04-05 20:09 +0200
Last post2012-04-06 10:43 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#13246 — setAttribute('type') = 'button'

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-04-05 20:09 +0200
SubjectsetAttribute('type') = 'button'
Message-ID<4f7ddff0$0$6869$e4fe514c@news2.news.xs4all.nl>
David Mark (8 nov 2011) wrote about setAttribute and argued that you should 
avoid using it (correct me if I've misunderstood).
He also pointed out that you can't trust el['type']='button' in some 
browsers.
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'.
My question is: how should I efficiently test for that condition?
TIA
Tom

[toc] | [next] | [standalone]


#13247

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-04-05 22:49 +0200
Message-ID<1398758.AFRqVoOGUt@PointedEars.de>
In reply to#13246
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>

[toc] | [prev] | [next] | [standalone]


#13258

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-04-06 10:43 +0200
Message-ID<4f7eacab$0$6929$e4fe514c@news2.news.xs4all.nl>
In reply to#13247
"Thomas 'PointedEars' Lahn" <PointedEars@web.de> schreef in bericht 
news:1398758.AFRqVoOGUt@PointedEars.de...
<snip>
>> 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.
>

You may be right. The xml in my example is a return message on a 
XMLHttpRequest.
Mostly it will inform about modifications, such as change color, hide, 
realign.
But there may be completely new html elements and that led to my problem 
when one such new element was an <input>.
It will be better then to split the xml into seperate sections: 1) a section 
with new DOM nodes, 2) one with modifications and 3) one with deleted nodes.
The new nodes can be imported - possibly replacing existing ones; the 
modifications and deletions get treatments analoguous to your widget.js.
The server can translate modifications to the type of <input> (however 
unlikely) into a new <input> element in the first section.
Great. Thank you.
Tom 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web