21

When viewing page source in Google Chrome, the browser opens a new tab and basically pastes the URL in with the view-source: prefix. This is undesirable.

As a developer, I may include some diagnostic output that is only visible in the source after submitting a form. When Chrome Refreshes the page to view the source, it makes this information disappear.

Is there anyway to prevent this behavior?

Note: I'm familiar with the "Inspect Element" option. This is just not an adequate stand-in for viewing the raw page source of the exact page you're looking at.


A quick test script

<pre>
  <?= print_r($_POST, true) ?>
</pre>
<form action="" method="post">
  <input id="foo" name="foo" value="bar" />
  <input type="submit" />
</form>

After clicking the submit button, the page shows

Array
(
    [foo] => bar
)

If you view page source, you will see an empty $_POST output

<pre>
Array
(
)
</pre>
<form action="" method="post"> 
  <input id="foo" name="foo" value="bar" /> 
  <input type="submit" /> 
</form> 

Update

Apparently this bug has already been submitted. Sigh...

If anyone knows of a good work around, I'd greatly appreciate it.

macek
  • 6,525

3 Answers3

12

From the bug report page, the workaround mentioned in comment 12 works: In the Developer Tools, enable Resource Tracking. (If it was off, enabling it will resubmit the request that generated the currently visible page, either POST or GET.) In the list of Resources, you can click on the main page to see the source code as it was returned by sever the for both POST and GET requests.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

More information

I ran some tests using a simple php file that showed the request method used and a POSTed value, a proxy server log to see which requests Chrome was making, and the chrome://net-internals/view-cache/ prefix to see what Chrome was caching.

When you use the View Source command, Chrome shows the source of its cached version of the page, and it only caches pages requested via the GET method.

If you're looking at a page that you've previously requested using GET and POST, then only the GET version is cached. Using the View Source command won't re-request the page, but will show the cached GET version, not the currently visible POST version, if any.

If you're looking at a page that you've only requested using the POST method, then using the View Source command will cause Chrome to look in its cache, find nothing, request the page using GET, cache it, and show the source of that.

Bavi_H
  • 6,710
2

Good question - and somewhat disappointing to read all those "this is wrong" or "this won't work" comments. This behavior makes the "View page source" feature useless for development in many cases.

There's an extension called "Quick source viewer", which appears to actually show the source of the currently loaded page (I haven't tested it with POST requests though).

basic6
  • 2,837
0

I'm sorry to tell you, but this is against the current nature of browsing and debugging in a browser...

The original source is not kept in memory, but is parsed and transformed into a parse tree as fast as possible, this to prevent useless memory usage. Thus, any debugging information you hide in the source is lost and must be explicitly requested. In the so-called Web 2.0 sites, elements also change and that is the reason the inspection is like that...

Solution 1: Fiddler Web Debugger allows you to inspect HTTP traffic,
this allows you to see the debug information from your last request.

Solution 2: Embed your debugging information or append it at the end,
or maybe show it as a pop-up or in another awesome way that doesn't disturb your lay-out.