I’m working on a side project at work which requires me to write some javascript code. Unfortunately I’m very new to the javascript world and am learning as I go.
So part of what I have to do requires me to find certain comments on the page, and I am using the following line to get all the comments first:
var arr = document.getElementsByTagName("!");
This works perfectly in IE8 and returns an array of all the comments on the page. However this returns an empty array in Google Chrome and I can see an uncaught TypeError (Presumably due to operations being performed on the empty array) when I open up the Developer Console.
Any ideas why this is not working on both browsers? Is this some sort of unsupported operation that I shouldn’t be using, or am I just misusing it?
Any help, or pointers in some direction are greatly appreciated! Thank you!
Clarification:
I am doing this in order to manipulate a sharepoint page. That actual intent of this script is to hide a table row that an input element is in. So to do that I need to first find the element, but I can’t do that since the attributes for it are basically garbled junk:
<input name="ctl00$m$g_edc51673_bad2_46d5_9a65_e71137e56558$ctl00$ctl04$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField"
type="text" value="Title" maxlength="255"
id="ctl00_m_g_edc51673_bad2_46d5_9a65_e71137e56558_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField"
title="Title" class="ms-long">
(I put in newlines in some places to make it easier to read, but that’s all on one line in the source (not that that makes a difference))
So anyway, I can’t really use that find the control I’m looking for. And you may say that that has some structure what with the type of the input consistently at the end of the garbage, and the title attribute and all that. But it isn’t. Yours truly, Microsoft, does this for some controls, but not for others.
BUT! They are absolutely consistent with providing comments right before this garbled junk that have the name of the element in them, and if I can grab that comment I can foo.parenNode over to what I want.
You can traverse the DOM starting at the node,
documentand iterating each node’schildNodesarray. When you find a node, you can test for thenodeType==8Here are links to
childNodes
nodeType
document.createTextNode
And a small example