dom - document not defined error javascript -
i'm learning javascript , have created html within javascript file test simple function takes in nodes parameters. keep getting 'document not defined' error. need within js file/code define document?
i tried things listed here: referenceerror: document not defined (in plain javascript)
var div1 = document.createelement("div"); var text1 = document.createtextnode('<div id="one">some<span>node <em>contents</em> for</span>comparison</div>'); div1.appendchild(text1); document.body.appendchild(div1); var div2 = document.createelement("div"); var text2 = document.createtextnode('<div id="two">some<span>node contents for</span>comparison</div>'); div2.appendchild(text2); document.body.appendchild(div2); var div3 = document.createelement("div"); var text3 = document.createtextnode('<div id="one">some<span>node <strong>contents</strong> for</span>comparison</div>'); div3.appendchild(text3); document.body.appendchild(div3); var div4 = document.createelement("div"); var text4 = document.createtextnode('<div id="four">some<span>node <em>contents</em> for</span>comparison</div>'); div4.appendchild(text4); document.body.appendchild(div4); function nodeequivalence(node1, node2) { var equal = false; if (node1.innerhtml === node2.innerhtml) { if (node1.tagname === node2.tagname) { if (node1.type === node2.type) { equal = true; } } } return equal; } console.log(nodeequivalence(div1, div4));
don't use innerhtml. use createtextnode or textcontent.
create html element right way:
var div = document.createelement("div"); var text = document.createtextnode("some text"); div.appendchild(text); document.body.appendchild(div);
your function complicated compare html nodes. function
function nodeequivalence(node1, node2) { var equal = false; if (node1.innerhtml === node2.innerhtml) { if (node1.tagname === node2.tagname) { if (node1.type === node2.type) { equal = true; } } } return equal; }
is easier.
Comments
Post a Comment