Getting the "unmodified" HTML document from Javascript
Is it possible to get the source of the current HTML document, exactly as it was loaded, in text form? (i.e. not the "Generated source" after parsing and DOM manipulation.)
Note: Issuing an extra AJAX开发者_运维技巧 request to retrieve the HTML page again is not an option in this case: The document could have changed.
Most browsers have a "view source" functionality, which would provide exactly what I want - so browsers keep the original HTML content anyway. Would be nice, if I could access that...
You can't do this with JavaScript, the browser has no responsibility to keep the original document really. Is making an AJAX request with a timestamp an option? You could store the loaded date of the page with new Date()
and pass this timestamp to the server when asking for the document again, if a history was available.
Other than that...I'm not sure how you'd do this with JavaScript/HTML. What is your actual end-game goal here though? Are you seeing if a <form>
and it's inputs changed, or something else?
As far as I know of, there is no way of doing so.
You may try grab the HTML very early and store it in a variable, but that's a very poor alternative because:
- if very early is too early (before all DOM nodes are loaded), you'll run into trouble trying to get the innerHTML property
- if very early is when the DOM is ready for manipulation, it might be too late already (if you have things like
<script>document.write(stuff);</script>
you may already seeing a different view over the HTML content)
Re-fetching the document with AJAX, despite its many possible implications, may be your best alternative regarding this matter.
A very bad hack-around method would be to load the page only using JS. Load a blank page with a single AJAX call to get the actual content of the page.
However, before doing that, I'd rethink what you are trying to do and why you need the "saved state."
精彩评论