Last updated on February 7th, 2022 at 01:04 pm
If you want to View source code of web page using Javascript then here is a simple example just copy and paste the script below to your webpage and press the View Source button.
<form action="#" onsubmit="return getsource();">
<input type="submit" value="View Source!" />
</form>
<script type="text/javascript" >
function getsource()
{
var thesource= window.location
//window.open(thesource, 'source_code', 'width=400, height=400, resizable=1, scrollbars=1');
window.location.href = "view-source:"+thesource;
return false;
}</script>
This script is no longer working in any browsers.Chrome and Firefox disabled this feature. More details on this link. They disabled it since its a security issue. Additional information on bugzilla about this.
If you use this script, you may encounter an error similar to
Uncaught TypeError: Location.href setter: Access to ‘view-source:’ from script denied.