Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript string with HTML --> node objects function?

    Has anyone seen a function that takes a string, parses any HTML, and returns it as an object with all its nodes?

    So, for example this…

    stringParser("<p>Here is the <strong>string</strong> we want to <em>add</em> to the page.</p>")

    …would return the elementNode 'p' with it's children the textNodes, elementNodes 'strong' and 'em' and their textNode children. The object could then be added to the document via appendChild.

    I'm thinking of developing a help system in which all of the help text is contained as an array in a liked js file, and when help is requested, the HTML is added to the DOM dynamically. Of course, if I don't act on the HTML string, it is printed verbatim in the browser.

    Thanks in advance,

    Brian

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cloneNode(true) does that, so inserting the strings into a documentFragment might work...
    *this message will self destruct in n-seconds*

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The best answer here is:
    - innerHTML
    - Mozilla specific createContextualFragment
    - Iew specific XMLDOM or DOMDocument
    - Moz/Op specific XMLSerializer
    - Opera supported W3C DOM3LS methods

    No generic solution exists, except for the first point in the list, and this only work for HTML (text/html) documents anyway.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    innerHTML was just what I was looking for, since I am only working with text/HTML. By creating my object and setting innerHTML to my HTML string, the object tree is "imported" as desired.

    Thanks so much for your quick response.

    Brian


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •