Categories:
Partners
Web Hosting
We've partnered with HostGator for reliable, affordable hosting starting at $3.95/mon. Use coupon code "javascriptkit25" for 25% off any package.

view-source:

By default, whenever we want to view the source of a document, we would reach for "view" of the toolbar of our browser. This "view" function can actually be simulated using a special kind of link- the view-source link. The standard syntax required is:

<a href="view-source:complete file path">Click to view the source!</a>

The path has to be complete, not simply the name of the file. Here are a couple of examples:

<a href=
"view-source:file:///C|/web/wa/index.htm">
Click to view the source!</a>
<a href=
"view-source:http://www.javascriptkit.com/index.htm">
Click to view the source!</a>

Here's where JavaScript comes in. Due to the fact that a html document can be viewed both online and offline (downloaded onto a harddrive), hard coding the complete path of a file to view source will produce a bad link in one of the two cases above. You may have entered http://www.javascriptkit.com/javatutors/links.htm in the view-source command while the surfer is viewing the page offline (file:///C|/web/javatutors/links.htm). To solve this little problem, simply use the window.location property of JavaScript to dynamically determine the current path of the html document:

<script>
function viewthesource(){
window.location="view-source:"+window.location
}
</script>
<a href="javascript:viewthesource()">
Click to view source!</a>

Click to view source!

Copyright 1997-2014 JavaScript Kit. NO PART may be reproduced without author's permission.