Question

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

Was it helpful?

Solution 2

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>

OTHER TIPS

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;

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top