Issue
I have this string that represents the body of a page, which I would like to parse for some elements. I believe (feel free to contradict me) the best way to do so is to create an empty document, then add the body and use standard JS methods to get what I want.
But I don't seem to be able to add the body to the document. In chrome, the following code fails on line 2 with NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7
.
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
dom.firstChild.innerHTML = "<body><p>Hello world</p></body>";
Is there any way to achieve what I want?
Solution
It's not possible to edit the innerHTML of the document's root element, but doing so of a child node is possible. So, this works:
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
var body = dom.createElement("body");
body.innerHTML = "<p>hello world</p>";
dom.firstChild.appendChild(body);
Answered By - Antoine Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.