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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to GET the source code of the document?

    I want to validate generated html using w3c validation service.

    I will need rendered source code as a string, in memory.

    innerHTML - rendered tag soup (e.g. for text/html, "<br>" when source code is "<br/>").
    view-source: + document.location - original (no generated HTML) source code

    How to get rendered source code as a string, in memory?

    Garrett

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    var d = document.documentElement.innerHTML;
    d=d.replace(/&/g,'&amp;');
    d=d.replace(/</g,'&lt;');
    d=d.replace(/>/g,'&gt;');

    and now d can be output to where ever you want the source to display.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    felgall's solution excludes the actual <html> tag itself. In IE, you can just grab document.documentElement.outerHTML, while in Firefox/Opera, call:
    Code:
    (new XMLSerializer()).serializeToString(document)
    That even grabs DOCTYPE and everything.


  •  

    Posting Permissions

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