8

Is it possible to get the html of the page with all modifications made by the user (see below)?

For example, if text has been entered into a textarea, or a checkbox has been chosen, that should be reflected in the html as well. My reason for wanting to do this is to take a "screenshot" of the user's page.

The answers below that are a variation of jQuery('html'), do not reflect changes made by the user.

LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
  • 2
    Duplicate of [javascript page source code](http://stackoverflow.com/q/1367587/218196). That's [the second question](http://stackoverflow.com/q/17450681/218196) about this within an hour. – Felix Kling Jul 03 '13 at 15:20
  • @FelixKling How is this a duplicate? The other question simply asks for the html of the page. I want text inputted in textareas, checkboxes selected, etc. to be reflected in the html – LanguagesNamedAfterCofee Jul 03 '13 at 15:22
  • Ah, I missed the `input`-changes part. The phrase *"modifications made by the user"* is a rather ambiguous. DOM modifications are modifications too (which could happen in response to user interaction). – Felix Kling Jul 03 '13 at 15:24
  • *related*: http://stackoverflow.com/q/11632238/218196 – Felix Kling Jul 03 '13 at 15:27
  • 1
    Really, an image of the page is what you where after? That has to be the poorest description of a problem in a question I've ever seen ? – adeneo Jul 03 '13 at 15:32
  • @adeneo I guess `to take a "screenshot" of the user's page` is a bad description? – LanguagesNamedAfterCofee Jul 03 '13 at 15:39
  • Not really, but `"Javascript get html of current page"` surely is. – adeneo Jul 03 '13 at 15:41

4 Answers4

8

If you're wanting a screenshot, you should consider a tool such as html2canvas. Just pulling the HTML isn't going to get you the input fields.

Another SO thread along these lines.

Other answers will get you the HTML, specifically, but without the inputs' values

$('html').html();
Community
  • 1
  • 1
Richard
  • 6,215
  • 4
  • 33
  • 48
5

You can use jQuery:

var html = $('html')
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
1

Adapting upon the other answers. Prior to obtaining the HTML, you could loop though all the input elements and manually set the value property, this would then be reflected in the HTML:

$('a').click(function(e){
    setValueProperties();
    var html = getHtml();
    alert(html);
});

function setValueProperties(){
    $('input').each(function(){
       $(this).attr("value", $(this).val()); 
    });
}

function getHtml(){
    return document.documentElement.outerHTML;
}

Here is a working example

And here is an example that supports checkboxes

NOTE: You may need to refine this function for your exact needs, but hopefully it will get you on track for what you need.

musefan
  • 47,875
  • 21
  • 135
  • 185
-1

If you have a field named "field":

var field = document.getElementById("field");
fieldvalue= field.value;

If you have a checkbox, the operation is literally the same. Hope this was helpful.