@Dean: Yeah, I was only about 50% serious with this one. Tobie and I were “in the shit” last night with a Prototype issue and after a while it became unclear how someElement.foo related to someElement.attributes.foo related to foo="bar" in the outerHTML. For a short period of time we were convinced that looking at the outerHTML gave you the best idea of which attributes were explicitly declared on an element.
Tobie and I both have enough material on this subject to fill several ardent blog posts, so I’m sure you’ll hear more ranting on this topic.
Boy do I look dumb… this is what happens when you’re one of the first people to look at a post, go away and leave a comment the next day without refreshing the page.
Nevertheless, if you really wanted to use outerHTML to do this sort of thing, you’d still want to do something like what I said.
This is
The weblog of Andrew Dupont, web interface developer and writer.
at 9:58 pm (Quote ↓)
I can’t believe you actually posted that. It’s hilarious! And it does work… kind of.
at 6:06 am (Quote ↓)
Yeah, hilarious … and broken ;P
If the element doesn’t have the attribute but any of its children has??
You can fix this by looking for indexOf(”>”) and comparing it to the one in the above example
at 9:35 am (Quote ↓)
This is better:
Element.Methods.Simulated.hasAttribute = function(element, attr) { return typeof element.attributes[attr] != “undefined”; };at 10:57 am (Quote ↓)
@Mislav: Damn. Good point.
@Dean: Yeah, I was only about 50% serious with this one. Tobie and I were “in the shit” last night with a Prototype issue and after a while it became unclear how
someElement.foorelated tosomeElement.attributes.foorelated tofoo="bar"in theouterHTML. For a short period of time we were convinced that looking at theouterHTMLgave you the best idea of which attributes were explicitly declared on an element.Tobie and I both have enough material on this subject to fill several ardent blog posts, so I’m sure you’ll hear more ranting on this topic.
at 11:51 am (Quote ↓)
There you go.
at 12:00 pm (Quote ↓)
The “kind of” bit is that you will register a false positive if any of the elements descendants also has the attribute.
I believe that if you want to make this work, you need something like (the code is probably buggy but you get the idea):
Element.Methods.Simulated.hasAttribute = function(element, attr) { return element.outerHTML.match(/^<[^>]*>/)[0] .indexOf(” ” + attr + “=”) > -1; };The idea is that you want to test the indexOf against only the markup of the element itself. Then the kind of bit mostly goes away.
at 12:08 pm (Quote ↓)
Boy do I look dumb… this is what happens when you’re one of the first people to look at a post, go away and leave a comment the next day without refreshing the page.
Nevertheless, if you really wanted to use outerHTML to do this sort of thing, you’d still want to do something like what I said.