How do I programmatically open the "View Source" window (using some Javascript) like when I right click in the browser and click "View Source"? Is this possible?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
There are 2 options [ and a workaround, which I will Explain later.. ]
#1. "HTML" From D.O.M
this is the 'safe' result, without the
<html>
wrapping anddoctype
:#2. HTML From Server (Through "Self-Ajax")
I've ran both in the console, in this current page (naturally before I've wrote this answer..)
and this is the side by side result, notice the difference.
notes and stuff..:
outerHTML
(but its not always available in every standard DOM)localStorage
, fork the code above, and make a small (and wonderful) script, place this script in the page'shead
just before you start modifying the page's source..You could do the following but it won't be the original HTML source: Loop through the DOM and re-create the source by outputting the properties/values of the nodes you find.
It is not an easy task (a huge one in fact), but it is pretty your only option.
Thanks
PS. I think this is what FF is doing, because there is always a subtle difference in the sources.
This will do it for broswers supporting the
view-source:
schemaA bookmarklet to do this can be made from the link in this scURIple:
Such a bookmarklet can be used on any page with a URI of an arbitrary schema and not just
http:
or pages that areHTML
based (and hence devoid of properties like.innerHTML
).Thus the URI for (these scURIples are amenable to immediate mode rendering):
can be examined directly with:
---------------------------------------------------------------------
Note: Making the above bookmarklet is an oxymoron and redundant since generally, browsers that support the
view-source:
schema (protocol) implement it directly in the user interface - however some interfaces are severely crippled which is why this bookmark is especially necessary when using:The Splashtop "Instant On" (not) environment by Device VM seriously amputates FF.
(hint: bookmark
to browse the Splashtop directory structure, generally ie:
view-source:file://media/
)---------------------------------------------------------------------
the devil made me do it - can't resist expounding upon:
including itself, observe the URI:
produces an obvious HTML source and so has an
.innerHTML
propertybut
is not an HTML source and so does not have an
.innerHTML
property.As for
Presumably, this is elementarily so - as long as the browser can render a page with a URI with a particular schema then that page must have an interpretable source that can be shown, ergo viewable, w/o an interpreted rendering.
(ie.
view-source:place:...
does not produce any viable page but then neither does aplace:...
URI though such a URI can be bookmarked - at least with FF [ v.>3.04? ] )so ...
The Simplest Thing To Do Is Use This 1 Line Of Javascript:
I Hope Its Compatible With Your Browser!
You could use this script, we simply grab the innerHTML of the html tag, reappend that, and paste that in a popup.
This will not completely show the source as it will not show anything outside the HTML tags, or any properties inside the html tag, but it should be close enough, and works cross-browser.
The advantage of this solution over the view-source: solution is that it will also work in internet-explorer 6> on windows XP SP2, that's pretty much it. If none of your audience is in this group, go with the view-source option, its way simpler.
You can use the "view-source" URI schema, supported by Firefox, Chrome, and older versions of IE.
No JavaScript required, just a normal link to the page you want the user to see in source view:
More info:
http://en.wikipedia.org/wiki/View-source