javascript - Auto add an anchor tag if currenttext === "@WebSecurity.CurrentUserName +<br>+ 'CP'" -
i'm trying add anchor in cell of specific table according text. here got far...
for (var d = 0, max = alltablecells.length; d < max; d++) { var node = alltablecells[d]; var currenttext = node.childnodes[0].nodevalue; if (currenttext === "@websecurity.currentusername + 'text'") node.childnodes[0].nodevalue = '<a name="cp"></a>' } }
the main issue here seems 2 lines...
if (currenttext === "@websecurity.currentusername + 'text'") node.childnodes[0].nodevalue = '<a name="cp"></a>'
if me, nice... thank you
here complete script of page: var alltablecells = document.getelementsbytagname("td");
for (var = 0, max = alltablecells.length; < max; i++) { var node = alltablecells[i]; var currenttext = node.childnodes[0].nodevalue; if (currenttext === "@websecurity.currentusername") node.style.color = "blue"; node.style.border = "solid" } (var c = 0, max = alltablecells.length; c < max; c++) { var node = alltablecells[c]; var currenttext = node.childnodes[0].nodevalue; if (currenttext === "none") node.style.color = "black"; node.style.border = "solid" } var d, max, node, nodes, currenttext, anchor; (d = 0, max = alltablecells.length; d < max; d++) { node = alltablecells[d]; nodes = node.childnodes; currenttext = nodes[nodes.length - 1].nodevalue; if (@websecurity.currentusername === nodes[0].nodevalue && currenttext === 'cp') { anchor = document.createelement('a'); anchor.name = 'cp'; alltablecells[d].insertbefore(anchor, nodes[0]); }
}
</script>
a cell <td>bob<br>text random<br>cp</td>
contains childnodes
below:
nodes = td.childnodes; nodes[0] === bob nodes[1] === br element nodes[2] === text random nodes[3] === br element nodes[4] === cp
notice, if </td>
not on same line cp
, there nodes[5] === empty text node
.
if text want check (said cp
in comment) in last node, need change code this:
var d, max, node, nodes, currenttext, currentuser, anchor; (d = 0, max = alltablecells.length; d < max; d++) { node = alltablecells[d]; nodes = node.childnodes; currentuser = parent.@websecurity.currentusername; currenttext = nodes[nodes.length - 1].nodevalue; if (currentuser === nodes[0].nodevalue && currenttext === 'cp') { anchor = document.createelement('a'); anchor.name = 'cp'; alltablecells[d].insertbefore(anchor, nodes[0]); } }
edit
in comments you've told code executed in iframe. assume @websecurity.currentusername
defined in main window. make sure it's global variable in main window, can use edited snippet above.
Comments
Post a Comment