How can I prevent users from Viewing my sites source and opening my JavaScript source file?
Asked
Active
Viewed 3,359 times
3
-
The answer may start with obfuscating. [Check out this related (or duplicate?) question's answers](http://stackoverflow.com/questions/194397/how-can-i-obfuscate-javascript). – Michael Dautermann Apr 19 '12 at 08:30
-
6You can't. Anything on the client side will always be visible to someone who wants to find it. You can however obfuscate it. – James Allardice Apr 19 '12 at 08:30
-
1Is it a question related to copyright? – Fabrizio Calderan Apr 19 '12 at 08:34
1 Answers
5
The user agents need to see and interpret the contents of the javascript. Hence unless your script belongs to a server side program (like node.js) you cannot prevent the user agent from accessing the script and in result the user from opening the javascript source file.
But, what you can do is to use obfuscation tools which would make your javascript code difficult to read for humans. With enough persistence even this can be reverse engineered. One example for such obfuscation tool is closure compiler.
So, you may maintain two versions. One version which is not obfuscated for debugging purposes and one for release mode. Also, note bugs in these obfuscation tools can lead to debugging night mares.

Ramesh
- 13,043
- 3
- 52
- 88