turn lump of text into a parseable document

Discussion in 'Javascript' started by james.kingston@gmail.com, Sep 24, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Sep 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    1
    Views:
    6,352
    Darren Davison
    Aug 5, 2003
  2. Lethal Possum
    Replies:
    6
    Views:
    422
    Lethal Possum
    Jul 6, 2006
  3. Thufir Hawat

    context.xml not parseable and valid

    Thufir Hawat, Feb 26, 2009, in forum: XML
    Replies:
    3
    Views:
    4,923
    stevedonato
    Jan 18, 2012
  4. Eric Osman
    Replies:
    2
    Views:
    350
    Eric Osman
    Apr 14, 2004
  5. Steve Kelem
    Replies:
    2
    Views:
    245
    John M. Gamble
    Aug 18, 2004
Loading...

Share This Page