Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question attribute values in Moz

    Mozilla returns an empty string for value & nodeValue... but how is it accessing the boolean value --> el[attName]?

    Code:
    <body>
    
    <script type="text/JavaScript">
    
      function expAtt(el, attName, attValue){
         var atts = el.attributes;
         atts.setNamedItem(document.createAttribute(attName));
         el[attName] = attValue;
         var att = atts[attName];
         alert(
           "element.attribute = " + typeof(el[attName]) + " : " + el[attName] + 
           "\n\nnodeValue = " + typeof(att.nodeValue) + " : " + att.nodeValue + 
           "\n\nvalue = " + typeof(att.value) + " : " + att.value
         )
      }
    </script>
    
    <button 
    onclick="expAtt(this, 'bool', true)">
    Test
    </button>
    
    </body>
    - compare the result in Internet Explorer...
    hmm... ?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The answer is simple: iew adds all attributes as properties of the element object. Moz and op only add those [DOM1HTML] and [DOM2HTML] specify as properties of the element object as properties of the element object. You should assign the attribute node a value before you assign it to hte Elmeent object.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the question:

    Where is the boolean value residing when its not specified as value|nodeValue?

    If its not a property of the element, why can the browser access it as element.attribute, or element["attribute"]?

    When the nodeValue is assigned directly (as element.attributes[attribute].nodeValue), the boolean value is converted to a string -- so that's no good.
    hmm... ?

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have a look at the innerHTML:
    IE assigns the attribute to the element;
    Mozilla does not, but will return a value for element.attribute
    Code:
    <body>
    
    <script type="text/JavaScript">
    
    function test(el, attName, attValue){
        el[attName] = attValue;
        var prop = document.getElementsByTagName("BUTTON")[0].bool;
        alert("element.bool = " + typeof(prop) + " : " + prop);
        alert(el.parentNode.innerHTML);
    }
    </script>
    <div>
    <button onclick="test(this, 'bool', true)">Test</button>
    </div>
    </body>
    Is this something like a global element variable, or what?
    hmm... ?

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, first of all, attribute values are strings, and nothing else. Any browser allowing non-string attribute values is wrong.

    What you must realise here is that you have two different things, the XML/HTML element, and the JavaScript object. The JavaScript object may have methods and properties, and the XML/HTML element may have attributes and content. Some attributes on the XML/HTML element are mapped to properties on the JavaScript object as per W3C DOM, but any other properties should be placed on the JavaScript object only. Attributes that are not mapped to properties per the W3C DOM should on the other hand not be exposed as properties of the JavaScript object. So, what iew does here is clearly faulty behavior.

    Another thing we have is naming. JavaScript and XML are both case sensitive, HTML is not, and should normalise everything to uppercase. JavaScript has a number of reserved words that are not reserved in HTML or XML. XML has a couple of reserved words that are not reserved in JavaScript. HTML and XML allows a more diverse set of characters in element and attribute names than JavaScript allows in identifiers. So, in conclusion, doing what iew is doing is not just wrong when compared to the specs, it's the opening of a can of worms that would have been better left closed.
    Last edited by liorean; 05-14-2004 at 12:52 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It seems that IE does this by design, as described in the parameters here: getAttribute Method

    Aside from that, however, I gather that the proper way to store non-string data (boolean, for example), as an element member, would be to set the attribute value as a string ("true" or "false"), and then retrieve it as Boolean(element.getAttribute(name))...

    - leaving it at that; thanks.
    - - - - - - - - - - - - - - - - -
    edit >> that should have been:
    ... set the attribute value as a string ("1" or "0"), and then retrieve it as Boolean(Number(element.getAttribute(name)))
    - leaving it at that
    Last edited by swmr; 05-14-2004 at 03:20 PM.
    hmm... ?

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yeah, but the behavior iew has is a hack because they didn't want to implement the DOM for real, they just wanted to add it as a layer on top of their own system.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow! Old post that I'm bringing back from the dead ( 2 years! ) 'cuz I've got such a noobish question, but it seems very similar to this discussion!

    I'm working on a Firefox web-app. (in-house intranet, so we've decided to go with FF only). I've requested an XML file from the server and am using it to parse a menu system. Originally I had separate node for each piece of data, but I later decided to shrink it down so that it uses attributes as well as nodes to hold data. My goal is to copy the values of the attributes as the values for attributes in the HTML DOM. For example, from the XML...
    Code:
    <menuitem title="Tooltip Text" id="item1" heading="Text of menu item">
      <subitem title="tooltip for subitem">
        <text>
          Subitem Text
        </text>
      </subitem>
    </menuitem>
    ... I'd like to make (coded in js but to save space, represented as HTML):
    Code:
    <dt title="Tooltip Text" id="item1">Text of menu item</dt>
    <dd title="tooltip for subitem">Subitem Text</dd>
    My question is, how can I get at the attribute values in js? Can I just say something like:
    Code:
    var menuItem = document.createElement('dt');
    menuItem.title=xmlNode.title;
    menuItem.appendChild(document.createTextNode(xmlNode.heading));
    htmlMenu.appendChild(menuItem);
    where I am addressing the attributes directly using dot-notation... I know menuItem.title should be fine since this is an acceptable attribute for an HTML node, but I'm not sure whether js would accept it for the xml node. Also I'm unsure how js would treat the non-standard attribute "heading". Or am I just better off to use the DOM method getAttribute() (esp. since this is in FF and FF handles this function correctly).

    Also, I'm more interested in the underlying reason why a particular approach would / wouldn't work, than I am in the actual answer to the question it should be a cinch to actually check this out on my own to see whether the js console is tossing errors or if the text in the menus are missing, etc.)
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by liorean
    Well, first of all, attribute values are strings, and nothing else. Any browser allowing non-string attribute values is wrong.
    There are cases in which attributes have no values, so that, in javascript, they accept Boolean. Beginers might be confused about that. Ex: options' selected
    attribute.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •