10

I tried using <a href="view-source:google.com">External Source</a> but that just returns a broken link.

Jonny
  • 860
  • 3
  • 12
  • 32
  • 1
    I believe that this will be browser specific. Is there a particular browser that you are targeting? – Thayne Dec 14 '13 at 02:22
  • 2
    Nope, just wondering if there was a standard for it. I just came across this tutorial which leads me to believe that it isn't possible without a hack http://css-tricks.com/make-a-view-source-button/ – Jonny Dec 14 '13 at 02:25

3 Answers3

3

If you're just going to need the HTML source of a webpage, and not anything else, and you're willing to use a server-side language, there is the option of using curl, file_get_contents, or Simple HTML DOM to get the HTML of a website, and then display that on your own page between <code></code> or <pre></pre> tags. This would look something like this in PHP

include("simplehtmldom.php");
$html=file_get_html($url);
echo "<pre>$html</pre>;

Obviously this should be formatted or prettyprinted. Take a look at Google Code prettifier to do this. If you want to get the source of your own page, you could use Javascript, and do this:

var html=document.documentElement.outerHTML;

I'm not sure how that would work for fetching external pages, but you could try an iframe for that, like this

document.getElementById('frame').contentWindow.documentElement.outerHTML;
scrblnrd3
  • 6,366
  • 9
  • 31
  • 61
  • I haven't used PHP lately, so please correct me if I'm wrong, but it looks like that would include the unescaped content directly into your output. Including source from an external site without escaping it is a serious security risk. – Nick Urban Dec 14 '13 at 04:12
  • @NickUrban I don't think so, especially if you're simply getting the html with `file_get_html` – scrblnrd3 Dec 14 '13 at 04:25
  • If you are injecting unescaped code, it could easily include – Nick Urban Dec 15 '13 at 00:05
  • @NickUrban good points, I'd forgotten about that. probably, you just want to replace all special chars like <>&"' with their entity numbers – scrblnrd3 Dec 15 '13 at 02:50
1

As an alternative to outputting everything in a <pre> block, consider returning a different content type. In your response headers:

Content-Type: text/plain

Then, you can simply return the HTML content and it will be displayed as plain-text in the browser.

Brad
  • 146,404
  • 44
  • 300
  • 476
1

The schema/protocol http: is missing:

<a href="view-source:http:google.com">External Source</a>`

Test with the scURIple (scriple):

data:text/html;charset=utf-8,<html><a href="view-source:http:google.com">External Source</a></html>
ekim
  • 36
  • 3
  • 6
    This doesn't work anymore. http://stackoverflow.com/questions/39755819/view-source-in-href-shows-error-in-console – Robert Mar 13 '17 at 16:41