<!--------------------------------------------------------------------------->
<!-- INTRODUCTION
The Code Project article submission template (HTML version)
Using this template will help us post your article sooner. To use, just
follow the 3 easy steps below:
1. Fill in the article description details
2. Add links to your images and downloads
3. Include the main article text
That's all there is to it! All formatting will be done by our submission
scripts and style sheets.
-->
<!--------------------------------------------------------------------------->
<!-- IGNORE THIS SECTION -->
<html>
<head>
<title>The Code Project</title>
<Style>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
<link rel="stylesheet" type=text/css href="http://www.codeproject.com/styles/global.css">
</head>
<body bgcolor="#FFFFFF" color=#000000>
<!--------------------------------------------------------------------------->
<!------------------------------- STEP 1 --------------------------->
<!-- Fill in the details (CodeProject will reformat this section for you) -->
<pre>
Title: A JavaScript Compression Tool for Web Applications
Author: Eric Woodruff
Email: Eric@EWoodruff.us
Environment: Visual Studio .NET, IIS, C#, ASP.NET, JavaScript
Keywords: ASP.NET, C#, JavaScript, compress, compact
Level: Intermediate
Description: A tool to compress JavaScript files to reduce their size and improve page load times.
Section Free Tools
SubSection Tools with Code
</pre>
<!------------------------------- STEP 2 --------------------------->
<!-- Include download and sample image information. -->
<ul class=download>
<li><a href='http://www.codeproject.com/csharp/jscompress/JSCompress.zip'>Download source and executables - 86 Kb</a></li>
</ul>
<!------------------------------- STEP 3 --------------------------->
<!-- Add the article text. Please use simple formatting (<h2>, <p> etc) -->
<h2>Introduction</h2>
<p>This article presents a JavaScript compression tool that takes your
JavaScript source code and compresses it by removing all comments,
extraneous whitespace, and optionally as many line feeds as possible, and
by optionally shortening function parameter and variable names. This will
reduce the script size and may help your pages load faster and reduce
bandwidth. A minor side benefit when line feed removal and variable
name compression is enabled is that it provides lightweight obfuscation of
the code making it harder for the casual user to read and/or play around with
it. It won't stop a determined user from reformatting and reverse
engineering it, but that is not the intent of this tool.</p>
<p>I developed this tool for use in my own ASP.NET projects. The code is
written in C# but as long as you have the .NET Framework installed, it can
be used to compress JavaScript for any web project, .NET or otherwise. The
supplied project file is for Visual Studio 2003 but it can be opened,
converted, and successfully compiled under Visual Studio 2005 as well.</p>
<p>There are three levels of compression:</p>
<ul>
<li><b>No Line Feed Removal</b>
<p>Line feeds are not removed from the script (except those deemed
extraneous such as on blank lines). Only comments and extraneous whitespace
is removed. This mode provides good compression and insures that no code is
broken.</p></li>
<li><b>Line Feeds Removed Wherever Possible</b>
<p>In this mode, line feeds are removed from the ends of statements in
which it is determined safe to do so, usually resulting in an extra 2% to
5% compression. For example, lines ending in an operator such as <code
lang=javascript>*</code>, <code lang=javascript>/</code>, <code
lang=javascript>+</code>, <code lang=javascript>-</code>, etc. and those
ending in a semi-colon will have any trailing line feeds removed. There are
several other conditions that can be met resulting in removal, and those
are described below in the code description sections. Steps are also taken
to prevent removal in instances such as missing semi-colons so as not to
break code. However, I may not have caught all such conditions so if code
is broken by this mode, you can fall back to the above mode. This mode
achieves its best results when you are diligent about putting semi-colons
after all statements that can use them to properly mark their
endpoints.</p></li>
<li><b>Function Parameter and Variable Name Compression</b>
<p>This can be combined with one of the first two compression options to
further reduce the script size. When enabled, as many function parameter
and variable names as possible will be renamed and shortened. The naming
scheme starts with the names <code>a</code> through <code>z</code>, then
<code>_a</code> through <code>_z</code>, <code>_aa</code> through
<code>_az</code>, <code>_ba</code> through <code>_bz</code>, etc. With
this option enabled, script size can usually be reduced by an additional
10% to 15%. There may be a higher potential for broken code with this
option so it is not enabled by default. If enabled, it is recommended that
you thoroughly test all compressed scripts before deploying them.</p></li></ul>
<p>Code blocks can also be surrounded by special <code>// #pragma
NoCompStart</code> and <code>// #pragma NoCompEnd</code> comments to
exclude sections from compression. This is useful for including copyright
notices in the header of compressed script files or skipping sections that
you are testing. For example:</p>
<pre lang=jscript>
// #pragma NoCompStart
//====================================================
// File : TestScript1.js
// Author : Eric Woodruff
// Updated : 07/23/2003
// #pragma NoCompEnd
// Anything from this point forward will be compressed
// .
// .
// .
// #pragma NoCompStart
// Skip compression on this section
function Test()
{
return true;
}
// #pragma NoCompEnd
// Resume compression
// .
// .
// .
</pre>
<p>The <code>#pragma</code> comments should appear on lines by themselves
and will be removed from the final compressed script. Any trailing comment
text on the same line as the <code>#pragma</code> is ignored and will be
removed as well. The compressor doesn't care about spacing or case on the
<code>#pragma</code> statements either.</p>
<h2>The Programs</h2>
<p>Two versions of the program are provided. The first is an interactive
version that you can use to test the different modes of compression. It is
a Windows Forms application written in C#. After running it, simply paste
your JavaScript code into the <i>Original Script</i> text box, turn the
<i>Line Feed Removal</i> and <i>Variable Name Compression</i> options on or
off, and click the <i>Compress</i> button. The compressed script is then
shown in the <i>Compressed Script</i> textbox with some compression
statistics displayed below it. The text can be copied to the clipboard from
the <i>Compressed Script</i> text box.</p>
<p>Note that when using the <i>Test only variable name compression</i>
option, the script code is not compressed. Only parameter and variable
names are compressed. This may help locate a problem with the variable
name compression code. Although the script code is not compressed,
comments are removed so that the naming results match (i.e. it won't use
different names due to matching a word that appears in a comment such as
"a", "be", or "to").
<p>The
一个高效的js脚本类库函数压缩工具程序源码
需积分: 9 162 浏览量
2008-09-18
12:39:31
上传
评论
收藏 84KB ZIP 举报
领君2018
- 粉丝: 204
- 资源: 1527
最新资源
- mnist手写数据集本地下载链接
- 52193906181644561181711717834527.jpg
- 图片缓冲技术(android)源码
- 花数据集+数据集汇总+标签txt+数据集汇总代码+迁移学习最佳模型+全部迭代最佳模型
- 20240329224412.zip
- switch.docx `switch`语句是C++中的一种流程控制语句,通常用于根据表达式的值选择执行不同的代码块 下面是`
- python绘制直方图-02-进程之间不共享全局变量.ev4.rar
- python绘制直方图-01-第三天知识点回顾.ev4.rar
- 01背包问题动态规划.docx
- 表达式求值.docx表达式求值涉及许多不同的情况和方法,具体取决于表达式的形式和要求的精度 下面是一个简单的例子
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈