Hidden javascript code
Hello I have some javascript code, a script included inside my page. I want to开发者_如何学编程 hide this script(obfuscate), so to make difficult to users to see the code with source view? Is there any way to do this?
EDIT: It's logically that expert users like all stackoverflows users, can always find the source.
For every obfuscator there exist a un-obfuscator.
If you have code you want to hide, use ajax and do the secret sauce on the server.
EDIT: It is possible to download javascript after page has been loaded and run that (this should work just fine to extend the runtime after the initial load, but the code is still transferred to the client and is possible to read for a human at the client end).
What I am trying to convey is that your critical code should only be run on the server and the result returned to the client.
Just use any minifier: Google Closure Compiler, and YUI compressor are the most popular.
These will remove all whitespace, comments, and change variable/function names to single-character names where possible.
Or, if you want to be a little more extreme, this tool will convert variable names and strings to Hex: http://www.javascriptobfuscator.com/default.aspx
The best you can do is to define your own obscure language, call it #
Then write two compilers, #
to JavaScript (this one needs to be in JavaScript) & JavaScript to #
Then just have a single file on the client that gets some source written in #
from the server and then compile / evaluate it line by line on the fly.
Of course #
needs to be obscure, and the JavaScript -> #
compiler is so that you can just write your code in JavaScript and pre compile it to #
and store it on the server.
It's not secure. People can read the compiler and figure out what the JavaScript equivalent is, but if you can find me a single person who cares enough to do that and I'll be impressed.
A minor side effect is that your seriously damaging performance on the client side merely due to an obsession with hiding your source code.
[Edit]
Of course we obscure the compiler with minimizers and ensure that the compiler, compiles & runs internally without converting to easily readable javascript anywhere.
There's nothing you can do to completely "protect" the code from being taken, read, understood, and used by anybody who can fetch your page short of legal action should you detect it being used in a way that you feel violates your copyright.
There are various compressors; I use YUIcompressor.
精彩评论