0

Possible Duplicate:
How can I obfuscate JavaScript?

Is there a way to protect JS file from being opened and copied. Perhaps it can be encoded? Just curious.

Community
  • 1
  • 1
santa
  • 12,234
  • 49
  • 155
  • 255
  • Disagree on possible duplicate. Obfuscation is the most realistic answer, but OP is asking for a way to prevent the user from viewing the JS at all. The question serves a different purpose (to explain that it can't be done). – Justin Morgan Apr 29 '11 at 20:02

3 Answers3

1

You can minify and obfuscate it but you cannot prevent it from being downloaded. There are many tools out there allowing you to achieve this. Some popular are the Google Closure Compiler and YUI Compressor.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    You can prevent it from being downloaded. But it won't do anything at the client side if it doesn't get there. –  Apr 29 '11 at 18:42
1

Obfuscating the Javascript code is your best bet if your goal is to prevent other users from stealing your javascript code. Although personally I think that is a waste of time.

Note that even obfuscated code can, to some extent, be de-obfuscated, and with enough time can be easily restored.

If it's your goal to hide information contained in your javascript file (passwords for example) then you are out of luck. These can be retrieved.

Lasar
  • 5,175
  • 4
  • 24
  • 22
  • I'd say 9 times out of ten, it'd take longer to parse through someone's obfuscated JS than just to write it out yourself. – SethWhite Nov 08 '16 at 22:17
1

No. JavaScript that cannot be copied by the user, cannot be run by the browser.

If you are concerned about intellectual property issues, your best bet aside from obfuscation is to architect your application in such a way that leaves most of the IP uniqueness of your product on the server side of your application.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83