ho il seguente codice che funziona in FF/ChromeIE solo javascript errore getElementsByTagName
var stack = [Array.prototype.slice.call(document.getElementsByTagName("body")[0].childNodes)], nodes, node, parent, text, offset;
while (stack.length) {
nodes = stack.pop();
for (var i=0, n=nodes.length; i<n; ++i) {
node = nodes[i];
switch (node.nodeType) {
case Node.ELEMENT_NODE:
if (node.nodeName.toUpperCase() !== "SCRIPT") {
stack.push(Array.prototype.slice.call(node.childNodes));
}
break;
case Node.TEXT_NODE:
text = node.nodeValue;
offset = text.indexOf("[test=");
if (offset >= 0 && text.substr(offset).match(/^(\[test=(\d+)\])/)) {
parent = node.parentNode;
var before = document.createTextNode(text.substr(0, offset));
link = document.createElement("a"),
after = document.createTextNode(text.substr(offset + RegExp.$1.length));
link.appendChild(document.createTextNode(text.substr(offset, RegExp.$1.length)));
link.setAttribute("href", "http://example.com/" + RegExp.$2);
parent.insertBefore(after, node);
parent.insertBefore(link, after);
parent.insertBefore(before, link);
parent.removeChild(node);
stack.push([after]);
}
}
}
}
Fondamentalmente ciò che fa è se trova [test = 25] nella pagina che lo converte in un link che indica example.com/25
in IE ottengo il seguente errore: JScript previsto oggetto sulla prima linea:
var stack = [Array.prototype.slice.call(document.getElementsByTagName("body")[0].childNodes)], nodes, node, parent, text, offset;
Questo errore si verifica sia in IE7 e IE8.
Qualsiasi aiuto sarebbe apprezzato.
Grazie.
+1. E puramente come una nota a margine: in alcuni benchmark JavaScript di Firefox che ho fatto di recente, ho scoperto che costruire un array usando un letterale vuoto '[]' e 'push' è, ironia della sorte, più veloce del metodo mostrato qui. –
Interessante! 'push' non può ricreare una lista sparsa (con elementi mancanti) come sopra, ma per una NodeList non ne avrai bisogno. – bobince
Grazie per l'informazione e soprattutto per il link a quell'altra domanda. Era esattamente quello che stavo cercando. – Rob