470,614 Members | 1,417 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

turn lump of text into a parseable document

In my first experimentation with js, I'm writing a greasemonkey script
which adds links to a page which, when clicked, will replace their
parent element with the contents of an element from another URL.

in the following function, pageHTML contains the entire text of an HTML
page, pulled in via GM_xmlhttprequest. The page contains a ul with
id="commentlisting". What I want to do, but can't seem to do, is turn
that lump of html in the pageHTML variable into a document that I can
run document.evaluate and document.getElementById on, just like I can
on the original page.

The second catch below alerts me with "TypeError:
tempnode.getElementById is not a function"
function getExpandedHTML(oldid, pageHTML)
{
GM_log("getExpanded started");
var tempNode;
try {
tempnode = document.createElement("document");
tempnode.innerHTML = pageHTML;
}
catch (e)
{
alert(e);
}
var listnode;
try {
listnode = tempnode.getElementById("commentlisting");
} catch (e)
{
alert(e);
}
//var innerlinks = listnode.evaluate("//a[@href]", listnode,
null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
//addlinkevenets(oldid, innerlinks);

GM_log("getExpanded finished");
return listnode.innerHTML;
}

Sep 24 '05 #1
1 1497


ja************@gmail.com wrote:

The second catch below alerts me with "TypeError:
tempnode.getElementById is not a function"
function getExpandedHTML(oldid, pageHTML)
{
GM_log("getExpanded started");
var tempNode;
try {
tempnode = document.createElement("document");
tempnode.innerHTML = pageHTML;
You are creating an _element_ node and set its innerHTML, you are not
creating a _document_ node. Thus that
listnode = tempnode.getElementById("commentlisting");


fails as only document nodes implement the method getElementById.

As far as I know currently browsers like Mozilla or Opera do not provide
an API to script to parse some string with HTML markup into an HTML
document so you might need to help yourself with creating an iframe and
writing the markup in there. But that will then for instance load images
or other stuff in the HTML so it is not an in memory operation.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 24 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Thomas W | last post: by
13 posts views Thread by Csaba Gabor | last post: by
8 posts views Thread by Michael Hill | last post: by
63 posts views Thread by John Salerno | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.