3

I'd like my web page to have a "Show Source" link that will show the source of my HTML.

I'm also wondering if there's something I could append to the URL of my page that'll just show the source as opposed to rendering the page. Like this...

http://www.example.com/mypage.html#show_source

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • I needed the equivalent of this 8 years later in 2020 and eventually came up with this: [Can I add a link to a web page which lets me view the source of that page?](https://stackoverflow.com/questions/63114428/can-i-add-a-button-or-a-link-to-my-web-page-which-lets-me-view-the-source-of-tha/63114429#63114429) – Rounin Jul 28 '20 at 17:48

2 Answers2

1

If you are okay with a link that opens source, you could use the following javascript:

if you are using FF:

window.location = "view-source:" + window.location.href;

and with IE:

var popup=window.open();
popup.document.open('text/plain').write(document.documentElement.outerHTML)

If all you need is code between the body tags - then you could do the following: document.body.innerHTML

Can you provide a bit more information on the application?

AshBrad
  • 482
  • 2
  • 15
0

"View Source Button" could be a solution, but if you need a direct link you need to change the Content-Type Header of your file to "plain/text".

If your page could be, for example, a PHP script, it would be:

<?php if($_GET["viewsource"]=="yes") header("Content-Type: plain/text"); ?>

and you can open link by appending ?viewsource=yes to your URL.

Remember, it works only "server side".