Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
View source - view the generated source of a page (140byt.es)

View source (140byt.es)

View the generated source of a page. 72 bytes.

Alternative version with non-english support (96 bytes):

function(){open('data:text/plain;charset=utf-8,'+encodeURI(document.documentElement.outerHTML))}
function(){
open(
'data:,'+encodeURI(
document.documentElement.outerHTML
)
)
}
function(){open('data:,'+encodeURI(document.documentElement.outerHTML))}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Sindre Sorhus <http://sindresorhus.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "viewSource",
"description": "View the generated source of a page.",
"keywords": [
"140bytes",
"view",
"source",
"html"
]
}
<!doctype html>
<title>viewSource</title>
<script>
var viewSource = function(){open('data:,'+encodeURI(document.documentElement.outerHTML))};
viewSource();
</script>
@atk

This comment has been minimized.

Copy link

@atk atk commented May 14, 2012

Doesn't show DOCTYPE; outerHTML not supported in all browsers. It could be possible to re-get the source by ajax request, yet this only works for those pages who are obtainable via GET request.

@sindresorhus

This comment has been minimized.

Copy link
Owner Author

@sindresorhus sindresorhus commented May 14, 2012

Doesn't need to show doctype, since it's only for viewing, though I could of course just prepend it.

outerHTML is supported in every browser that is usually supported, but you don't specify which you require:

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 11.0 (11) 0.2 4.0 7 1.3
@atk

This comment has been minimized.

Copy link

@atk atk commented May 14, 2012

That only leaves out pre-11 Firefox Versions, which are still in use out there. Nice enough.

@skylamer

This comment has been minimized.

Copy link

@skylamer skylamer commented May 14, 2012

or just use view-source:<url> :D)

@williammalo

This comment has been minimized.

Copy link

@williammalo williammalo commented May 14, 2012

You can just do this:

function(){open('data:,'+encodeURI(document.documentElement.outerHTML))}

Much shorter...

@sindresorhus

This comment has been minimized.

Copy link
Owner Author

@sindresorhus sindresorhus commented May 14, 2012

@williammalo You're absolutely right, thanks :)

I have no idea why my brain insisted on double escaping...

@jed

This comment has been minimized.

Copy link

@jed jed commented May 15, 2012

how about instead:

function(){with(document)body.innerText=documentElement.outerHTML}
@subzey

This comment has been minimized.

Copy link

@subzey subzey commented May 15, 2012

Data URL's default MIME is text/plain;charset=us-ascii while encodeURI returns escaped Utf-8 string. So non-english text will most probably be “broken”.

@fhemberger

This comment has been minimized.

Copy link

@fhemberger fhemberger commented May 15, 2012

Use it as a bookmarklet:

<!doctype html> 
<title>viewSource</title>
<a href="javascript:(function(){open('data:,'+encodeURI(document.documentElement.outerHTML))})()">view source</a>
@sindresorhus

This comment has been minimized.

Copy link
Owner Author

@sindresorhus sindresorhus commented May 15, 2012

@jed I could, but I wanted it to open in a new window, so not to destroy the site you're on. Firefox doesn't support innerText, so you would need to use textContent.

@subzey I didn't know that, but you are correct. Added an alternative version.

@jed

This comment has been minimized.

Copy link

@jed jed commented May 16, 2012

nice work, @sindresorhus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.