We all know that JavaScript is an interpreted language executed in the client browser. In other words, JavaScript will be downloaded to the browser in plain text, and then the browser will execute the JavaScript code as needed. You only need to use the browser's function to view the source code to read the JavaScript source code. This function will display the complete HTML mark of the page,
We all know that JavaScript is an interpreted language executed in the client browser. In other words, JavaScript will be downloaded to the browser in plain text, and then the browser will execute the JavaScript code as needed.
You only need to use the browser's source code view function to read the JavaScript source code. This function displays the complete HTML mark of the page, including all JavaScript blocks. Even if the JavaScript source code is put in an external file and referenced by the src attribute marked by the script, you can download and read it. Because anyone who looks at the page can get the JavaScript source code, do not put private or confidential logic algorithms in JavaScript. It is best to put this logic on the server, where it will be safer.
In Ajax-based applications, as JavaScript is used more and more, the size of JavaScript files may become a problem. Because JavaScript is an interpreted language, it is not compiled as a machine-level binary code. For executable code, binary code is a more efficient storage format. If there are too many JavaScript files, the application will slow down because it needs to download the source code from the server to the browser before it can be executed in the browser. In addition, if you use a tool such as JSDoc (as described above) to annotate the code, a large JavaScript code will become larger.
As you may see, JavaScript lacks a binary executable package, which brings two problems: poor security and the need to download a large amount of source code. Is there a way to avoid these problems?
JavaScript has become increasingly popular, so many tools have been generated that help solve these problems. The simplest compression tool will simply remove all comments and line breaks in the JavaScript source code, which can reduce the size of the downloaded source code. Deleting comment lines and line breaks can reduce the JavaScript file size by 30% or more, depending on the specific situation. It should be noted that all statements in JavaScript source code must end with a semicolon correctly. Only in this way can we use this tool to compress the source code. If this is not done, you will receive messages of errors or unexpected behaviors. Therefore, before compressing JavaScript source code, you must use JSLint to ensure that all statements end with a semicolon!
Some other tools are further used to provide fuzzy services. Fuzzy (Obfuscation) is a process that comprehensively scans the source code and changes the original names of fields and functions to meaningless names encoded, to prevent others from understanding the meaning of source code and internal work. For languages that can be compiled into machine-level binary commands (such as C ++), fuzzy processing is generally not required. Even if Java and C # can be compiled as intermediate bytecode instead of binary instructions in modern languages, fuzzy tools are also required to ensure maximum security. JavaScript, as a fully interpreted language, also needs such a tool.
There is a free tool that provides both compression and fuzzy services, that is, MemTronic's HTML/JavaScriptCruncher-Compressor (hometown.aol.de/_ht_a/memtronic /). This tool supports multiple levels of JavaScript compression. The lowest level of compression is called crunching in this tool, but it simply deletes all comments and line breaks. This tool can save 20% ~ Bandwidth of 50%. Using the "crunch" mode, we can see that the JavaScript file size is reduced by 30%.
The highest level of compression is called compressing in this tool. It uses a real compression mechanism to actually compress JavaScript source code and adds the automatic decompression function to the file. According to related documents of this tool, when using this mode, the bandwidth can be reduced by 40% ~ 90%, and the compressed output has been successfully tested on IE, Netscape, Mozilla, Opera, and other browsers of the current version. Using the same JavaScript file, the "compressing" mode and the "crunch" mode were used for testing. It was found that the "compress" Mode reduced the file size by more than 65% (see-13 ).
When writing this book, The MemTronic tool documentation said that the JavaScript fuzzy tool is not complete yet. However, let's take a look at the output window shown in-13, where the result of the "compression" operation on JavaScript files is displayed. This output contains many strange characters that are difficult to read. Although this may not be a true blur, it is enough to prevent users with bad attempts from viewing (or even stealing) Your JavaScript source code.
MemTronic's HTML/JavaScriptCruncher-Compressor can greatly reduce the JavaScript source code size and make it difficult to understand