Load HTML in text strings into HTML parser in Javascript

Discussion in 'HTML' started by David Virgil Hobbs, Apr 9, 2006.

  1. Loading text strings containing HTML code into an HTML parser in a
    Javascript/Jscript

    I would like to know, how one would go about loading a text string
    containing HTML code, so as to be able to use javascript or Jscript to
    work with the HTML code in the text string, in the same way that one
    works with XML code in a text string using the XML parser.

    If I was able to load the text string containing the HTML code
    succesfully, I would be able to for example, find what
    document.someform.someinput.value is in the loaded text string
    containing HTML, the same way I can use javascript to find what
    document.someform.someinput.value is in the HTML that is on the same
    page as the javascript.

    Microsoft's xml parser, allows you to load a text string containing xml
    code, such as text in an html form input, and then use javascript to
    work with this text string containing xml code, as if you were working
    with an xml.

    The syntax for this is, var xmlDoc=new
    ActiveXObject("MICROSOFT.XMLDOM"),
    xmlDoc.loadXML(document.form.input.value).

    Thus, you can for example, find xmlDoc.childNodes(0).nodeName etc etc
    inside the text string that is loaded into the Parser.

    It has been surprisingly tough going trying to figure out how to do
    this if possible, using search engines. Seems being able to do what I
    seek to do, is fundamental to the teaching and learning of
    HTML/DHTML/CSS/Javascript.
     
    David Virgil Hobbs, Apr 9, 2006
    #1
    1. Advertising

  2. David Virgil Hobbs

    Ian Collins Guest

    David Virgil Hobbs wrote:
    > Loading text strings containing HTML code into an HTML parser in a
    > Javascript/Jscript
    >
    > I would like to know, how one would go about loading a text string
    > containing HTML code, so as to be able to use javascript or Jscript to
    > work with the HTML code in the text string, in the same way that one
    > works with XML code in a text string using the XML parser.
    >

    Have you tried creating a DOM element and setting it's innerHTML to your
    string?

    --
    Ian Collins.
     
    Ian Collins, Apr 9, 2006
    #2
    1. Advertising

  3. David Virgil Hobbs

    RobG Guest

    David Virgil Hobbs wrote:
    > Loading text strings containing HTML code into an HTML parser in a
    > Javascript/Jscript
    >
    > I would like to know, how one would go about loading a text string
    > containing HTML code, so as to be able to use javascript or Jscript to
    > work with the HTML code in the text string, in the same way that one
    > works with XML code in a text string using the XML parser.


    A JavaScript function posted about a week ago (it lacks feature
    detection and is only lightly tested):

    function toDOM(HTMLstring)
    {
    var d = document.createElement('div');
    d.innerHTML = HTMLstring;
    var docFrag = document.createDocumentFragment();

    while (d.firstChild) {
    docFrag.appendChild(d.firstChild)
    };

    return docFrag;
    }

    <URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/d9855d899b8a6544/4c4071600634c1ad?q=HTML+to+DOM+Function&rnum=1#4c4071600634c1ad>


    [...]

    > Microsoft's xml parser, allows you to load a text string containing xml
    > code, such as text in an html form input, and then use javascript to
    > work with this text string containing xml code, as if you were working
    > with an xml.


    The W3C DOM 3 Load and Save includes interfaces for parsing and
    serialising XML, however they aren't widely implemented yet.

    <URL:http://www.w3.org/TR/DOM-Level-3-LS/load-save.html>


    [...]



    --
    Rob
     
    RobG, Apr 9, 2006
    #3
    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. Jeremy Chapman

    Load contents of a text file into a text box

    Jeremy Chapman, Aug 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    764
    Tommy
    Aug 15, 2003
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  3. mike b.
    Replies:
    3
    Views:
    165
    James Edward Gray II
    Jul 30, 2007
  4. Andy Burghardt

    Checkbox to load text into text area

    Andy Burghardt, Jan 31, 2008, in forum: Javascript
    Replies:
    2
    Views:
    159
    Andy Burghardt
    Jan 31, 2008
  5. Duke
    Replies:
    1
    Views:
    166
    Tom de Neef
    Apr 18, 2008
Loading...

Share This Page