开发者

Show html code of web page when pressing an html button

Is it possible in html/javascript to return the html code of a page?

I开发者_如何学C have an html page, and i would like to, by a click on a input[type=button], open the source code of the page.


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

That should work fine in Firefox and Chrome. It won't work in Internet Explorer though. Another option is to use the outerHTML property:

var source = document.documentElement.outerHTML;

And that should work fine in most browsers. To get that open in a new window, you could do something like this:

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


Interesting idea! All I can think of is:

For Chrome: try opening the url "view-source:[original url]" in another tab. Might work.

Or to be fancy, use php or ajax to copy the html into a txt file and link to that? Or display it in a block or something...

Or tell the user to press ctrl+U?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜