开发者

How to prevent View Source of page using Javascript?

How to prevent to download the so开发者_StackOverflow社区urce code of page using File > Save as?


It's not possible and you shouldn't even bother trying it, because:

  • The users who realize, there is a source, will most likely have the knowledge to download it anyway.
  • The normal users, who are unaware of the whole source concept are only annoyed by the futile attemps to block the source ("Why is my right mouse button not working on this site? All I want to do is create a bookmark.")

Just look at Microsoft, Amazon, and Co., but also at sites like DeviantArt: All these businesses obviously spent a lot of effort (=money) into their sites, and they don't do anything to prevent the source access.

On the other hand I've seen many small private sites, with no real content or value other to the creator of the pages, which try to prevent accessing their source.

As a rule of thumb: Any kind of copy control technique, which harasses your legitimate customers, doesn't pay off and will damage your business in the long run.


You cannot prevent this behavior. Even if you were to be able to prevent it in any particular browser, you are still delivering code content that browsers render for you, someone could just create a raw request and view the source.


As a more constructive answer, if you're talking about stopping people downloading your javascript file, you can't, however you try obfuscating/confusing/packing the script text into something pretty much unreadable.

Try this javascript obfuscator. It seems to work pretty well!

As a note, this won't stop someone who is determined, but will certainly really slow them down.


You can't. The page is effectively already being downloaded when you view it in the browser. If you want to protect the contents, you might be able to obfuscate the source, and depending on what you are doing you might be able to make the page non-functional once it has been downloaded (ie making some behavior of the page rely on an Ajax request to your server). But there will always be workarounds.


<!-- Right Click Disable -->
<script type="text/javascript">
<!-- 
var message=""; 
function clickIE() {if (document.all) {(message);return false;}} 
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) { 
if (e.which==2||e.which==3) {(message);return false;}}} 
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;} 
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;} 

document.oncontextmenu=new Function("return false") 
</script>
<!-- Right Click Disable -->
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜