下面是压缩后的JS:
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('m 6(a,b,c,d,e){5.u=6.$(a);7(5.u==k&&a!="n"){M y N("6(p)参数错误:p 对像存在!(Z:"+a+")")};7(!6.q){6.q=y 1r()};5.p=6.q.9;6.q.1d(5);5.1s=r;5.8=[];5.10=c==k?0:c;5.E=5.10;5.11=d==k?"1t":d;5.13=e==k?"":e;5.F=r;j f=P("6.q["+5.p+"].F = z");j g=P("6.q["+5.p+"].F = r");7(a!="n"){7(5.u.v){5.u.v("14",f)}w{5.u.Q("15",f,r)}};7(a!="n"){7(5.u.v){5.u.v("16",g)}w{5.u.Q("17",g,r)}};7(18(b)!="1u"){b="1v"};b=b.1w();1x(b){U"14":5.B="15";G;U"16":5.B="17";G;U"1y":5.B="1z";G;U"1A":5.B="1B";G;1C:5.B="1D"};5.H=r;5.A=k;5.R=1E};6.t.1F="1.1G";6.t.1H="1I";6.t.1e=m(a,b,c,d,e){7(6.$(a)==k&&a!="n"){M y N("1e(1f)参数错误:1f 对像存在!(Z:"+a+")")};j f=5.8.9;7(c==""){c=k};5.8.1d([a,b,c,d,e]);j g=P(\'6.q[\'+5.p+\'].C(\'+f+\')\');7(a!="n"){7(6.$(a).v){6.$(a).v("1J"+5.B,g)}w{6.$(a).Q(5.B,g,r)}};7(f==5.10){7(a!="n"){6.$(a).V=5.11};7(6.$(b)){6.$(b).J.W=""};7(5.p!="n"){7(c!=k){5.u.J.1g=c}};7(d!=k){S(d)}}w{7(a!="n"){6.$(a).V=5.13};7(6.$(b)){6.$(b).J.W="n"}};j h=P("6.q["+5.p+"].F = z");j i=P("6.q["+5.p+"].F = r");7(6.$(b)){7(6.$(b).v){6.$(b).v("14",h)}w{6.$(b).Q("15",h,r)};7(6.$(b).v){6.$(b).v("16",i)}w{6.$(b).Q("17",i,r)}}};6.t.C=m(a,b){7(18(a)!="19"){M y N("C(1h)参数错误:1h 不是 19 类型!(Z:"+a+")")};7(b!=z&&5.E==a){K};j i;T(i=0;i<5.8.9;i++){7(i==a){7(5.8[i][0]!="n"){6.$(5.8[i][0]).V=5.11};7(6.$(5.8[i][1])){6.$(5.8[i][1]).J.W=""};7(5.p!="n"){7(5.8[i][2]!=k){5.u.J.1g=5.8[i][2]}};7(5.8[i][3]!=k){S(5.8[i][3])}}w 7(5.E==i||b==z){7(5.8[i][0]!="n"){6.$(5.8[i][0]).V=5.13};7(6.$(5.8[i][1])){6.$(5.8[i][1]).J.W="n"};7(5.8[i][4]!=k){S(5.8[i][4])}}};5.E=a};6.t.1a=m(){7(s.9!=5.8.9){M y N("1a()参数错误:参数数量与标签数量不符!(9:"+s.9+")")};j a=0,i;T(i=0;i<s.9;i++){a+=s[i]};j b=1K.1a(),1b=0;T(i=0;i<s.9;i++){1b+=s[i]/a;7(b<1b){5.C(i);G}}};6.t.1i=m(){7(s.9!=5.8.9){M y N("1i()参数错误:参数数量与标签数量不符!(9:"+s.9+")")};7(!(/^\\d+$/).1j(6.D)){K};j a=0,i;T(i=0;i<s.9;i++){a+=s[i]};j b=6.D%a;7(b==0){b=a};j c=0;T(i=0;i<s.9;i++){c+=s[i];7(c>=b){5.C(i);G}}};6.t.1L=m(a){7(18(a)=="19"){5.R=a};X(5.A);5.A=1c("6.q["+5.p+"].Y()",5.R);5.H=z};6.t.Y=m(){7(5.H==r||5.F==z){K};5.1k()};6.t.1k=m(){j a=5.E;a++;7(a>=5.8.9){a=0};5.C(a);7(5.H==z){X(5.A);5.A=1c("6.q["+5.p+"].Y()",5.R)}};6.t.1M=m(){j a=5.E;a--;7(a<0){a=5.8.9-1};5.C(a);7(5.H==z){X(5.A);5.A=1c("6.q["+5.p+"].Y()",5.R)}};6.t.1N=m(){X(5.A);5.H=r};6.$=m(a){7(x.1l){K S(\'x.1l("\'+a+\'")\')}w{K S(\'x.1O.\'+a)}};6.1m=m(l){j i="",I=l+"=";7(x.L.9>0){j a=x.L.1n(I);7(a!=-1){a+=I.9;j b=x.L.1n(";",a);7(b==-1)b=x.L.9;i=1P(x.L.1Q(a,b))}};K i},6.1o=m(O,o,l,I){j i="",c="";7(l!=k){i=y 1p((y 1p).1R()+l*1S);i="; 1T="+i.1U()};7(I!=k){c=";1V="+I};x.L=O+"="+1W(o)+i+c};6.D=6.1m("1q");7((/^\\d+$/).1j(6.D)){6.D++}w{6.D=1};6.1o("1q",6.D,12);',62,121,'|||||this|SubShowClass|if|label|length||||||||||var|null||function|none||ID|childs|false|arguments|prototype|parentObj|attachEvent|else|document|new|true|autoPlayTimeObj|eventType|select|sum|selectedIndex|mouseIn|break|autoPlay||style|return|cookie|throw|Error||Function|addEventListener|spaceTime|eval|for|case|className|display|clearInterval|autoPlayFunc|value|defaultID|openClassName||closeClassName|onmouseover|mouseover|onmouseout|mouseout|typeof|number|random|percent|setInterval|push|addLabel|labelID|background|num|order|test|nextLabel|getElementById|readCookie|indexOf|writeCookie|Date|SSCSum|Array|lock|selected|string|onmousedown|toLowerCase|switch|onclick|click|onmouseup|mouseup|default|mousedown|5000|version|30|author|mengjia|on|Math|play|previousLabel|stop|all|unescape|substring|getTime|3600000|expires|toGMTString|domain|escape'.split('|'),0,{}))
格式化后是这样的:
function SubShowClass(a, b, c, d, e) { this.parentObj = SubShowClass.$(a); if (this.parentObj == null && a != "none") { throw new Error("SubShowClass(ID)参数错误:ID 对像存在!(value:" + a + ")") }; if (!SubShowClass.childs) { SubShowClass.childs = new Array() }; this.ID = SubShowClass.childs.length; SubShowClass.childs.push(this); this.lock = false; this.label = []; this.defaultID = c == null ? 0 : c; this.selectedIndex = this.defaultID; this.openClassName = d == null ? "selected" : d; this.closeClassName = e == null ? "" : e; this.mouseIn = false; var f = Function("SubShowClass.childs[" + this.ID + "].mouseIn = true"); var g = Function("SubShowClass.childs[" + this.ID + "].mouseIn = false"); if (a != "none") { if (this.parentObj.attachEvent) { this.parentObj.attachEvent("onmouseover", f) } else { this.parentObj.addEventListener("mouseover", f, false) } }; if (a != "none") { if (this.parentObj.attachEvent) { this.parentObj.attachEvent("onmouseout", g) } else { this.parentObj.addEventListener("mouseout", g, false) } }; if (typeof (b) != "string") { b = "onmousedown" }; b = b.toLowerCase(); switch (b) { case "onmouseover": this.eventType = "mouseover"; break; case "onmouseout": this.eventType = "mouseout"; break; case "onclick": this.eventType = "click"; break; case "onmouseup": this.eventType = "mouseup"; break; default: this.eventType = "mousedown" }; this.autoPlay = false; this.autoPlayTimeObj = null; this.spaceTime = 5000 }; SubShowClass.prototype.version = "1.30"; SubShowClass.prototype.author = "mengjia"; SubShowClass.prototype.addLabel = function (a, b, c, d, e) { if (SubShowClass.$(a) == null && a != "none") { throw new Error("addLabel(labelID)参数错误:labelID 对像存在!(value:" + a + ")") }; var f = this.label.length; if (c == "") { c = null }; this.label.push([a, b, c, d, e]); var g = Function('SubShowClass.childs[' + this.ID + '].select(' + f + ')'); if (a != "none") { if (SubShowClass.$(a).attachEvent) { SubShowClass.$(a).attachEvent("on" + this.eventType, g) } else { SubShowClass.$(a).addEventListener(this.eventType, g, false) } }; if (f == this.defaultID) { if (a != "none") { SubShowClass.$(a).className = this.openClassName }; if (SubShowClass.$(b)) { SubShowClass.$(b).style.display = "" }; if (this.ID != "none") { if (c != null) { this.parentObj.style.background = c } }; if (d != null) { eval(d) } } else { if (a != "none") { SubShowClass.$(a).className = this.closeClassName }; if (SubShowClass.$(b)) { SubShowClass.$(b).style.display = "none" } }; var h = Function("SubShowClass.childs[" + this.ID + "].mouseIn = true"); var i = Function("SubShowClass.childs[" + this.ID + "].mouseIn = false"); if (SubShowClass.$(b)) { if (SubShowClass.$(b).attachEvent) { SubShowClass.$(b).attachEvent("onmouseover", h) } else { SubShowClass.$(b).addEventListener("mouseover", h, false) }; if (SubShowClass.$(b).attachEvent) { SubShowClass.$(b).attachEvent("onmouseout", i) } else { SubShowClass.$(b).addEventListener("mouseout", i, false) } } }; SubShowClass.prototype.select = function (a, b) { if (typeof (a) != "number") { throw new Error("select(num)参数错误:num 不是 number 类型!(value:" + a + ")") }; if (b != true && this.selectedIndex == a) { return }; var i; for (i = 0; i < this.label.length; i++) { if (i == a) { if (this.label[i][0] != "none") { SubShowClass.$(this.label[i][0]).className = this.openClassName }; if (SubShowClass.$(this.label[i][1])) { SubShowClass.$(this.label[i][1]).style.display = "" }; if (this.ID != "none") { if (this.label[i][2] != null) { this.parentObj.style.background = this.label[i][2] } }; if (this.label[i][3] != null) { eval(this.label[i][3]) } } else if (this.selectedIndex == i || b == true) { if (this.label[i][0] != "none") { SubShowClass.$(this.label[i][0]).className = this.closeClassName }; if (SubShowClass.$(this.label[i][1])) { SubShowClass.$(this.label[i][1]).style.display = "none" }; if (this.label[i][4] != null) { eval(this.label[i][4]) } } }; this.selectedIndex = a }; SubShowClass.prototype.random = function () { if (arguments.length != this.label.length) { throw new Error("random()参数错误:参数数量与标签数量不符!(length:" + arguments.length + ")") }; var a = 0, i; for (i = 0; i < arguments.length; i++) { a += arguments[i] }; var b = Math.random(), percent = 0; for (i = 0; i < arguments.length; i++) { percent += arguments[i] / a; if (b < percent) { this.select(i); break } } }; SubShowClass.prototype.order = function () { if (arguments.length != this.label.length) { throw new Error("order()参数错误:参数数量与标签数量不符!(length:" + arguments.length + ")") }; if (!(/^\d+$/).test(SubShowClass.sum)) { return }; var a = 0, i; for (i = 0; i < arguments.length; i++) { a += arguments[i] }; var b = SubShowClass.sum % a; if (b == 0) { b = a }; var c = 0; for (i = 0; i < arguments.length; i++) { c += arguments[i]; if (c >= b) { this.select(i); break } } }; SubShowClass.prototype.play = function (a) { if (typeof (a) == "number") { this.spaceTime = a }; clearInterval(this.autoPlayTimeObj); this.autoPlayTimeObj = setInterval("SubShowClass.childs[" + this.ID + "].autoPlayFunc()", this.spaceTime); this.autoPlay = true }; SubShowClass.prototype.autoPlayFunc = function () { if (this.autoPlay == false || this.mouseIn == true) { return }; this.nextLabel() }; SubShowClass.prototype.nextLabel = function () { var a = this.selectedIndex; a++; if (a >= this.label.length) { a = 0 }; this.select(a); if (this.autoPlay == true) { clearInterval(this.autoPlayTimeObj); this.autoPlayTimeObj = setInterval("SubShowClass.childs[" + this.ID + "].autoPlayFunc()", this.spaceTime) } }; SubShowClass.prototype.previousLabel = function () { var a = this.selectedIndex; a--; if (a < 0) { a = this.label.length - 1 }; this.select(a); if (this.autoPlay == true) { clearInterval(this.autoPlayTimeObj); this.autoPlayTimeObj = setInterval("SubShowClass.childs[" + this.ID + "].autoPlayFunc()", this.spaceTime) } }; SubShowClass.prototype.stop = function () { clearInterval(this.autoPlayTimeObj); this.autoPlay = false }; SubShowClass.$ = function (a) { if (document.getElementById) { return eval('document.getElementById("' + a + '")') } else { return eval('document.all.' + a) } }; SubShowClass.readCookie = function (l) { var i = "", I = l + "="; if (document.cookie.length > 0) { var a = document.cookie.indexOf(I); if (a != -1) { a += I.length; var b = document.cookie.indexOf(";", a); if (b == -1) b = document.cookie.length; i = unescape(document.cookie.substring(a, b)) } }; return i }, SubShowClass.writeCookie = function (O, o, l, I) { var i = "", c = ""; if (l != null) { i = new Date((new Date).getTime() + l * 3600000); i = "; expires=" + i.toGMTString() }; if (I != null) { c = ";domain=" + I }; document.cookie = O + "=" + escape(o) + i + c }; SubShowClass.sum = SubShowClass.readCookie("SSCSum"); if ((/^\d+$/).test(SubShowClass.sum)) { SubShowClass.sum++ } else { SubShowClass.sum = 1 }; SubShowClass.writeCookie("SSCSum", SubShowClass.sum, 12);
后面那些用|隔开的是什么,有什么用?
这个不叫压缩,但也可以算作一种压缩。
不过是把里面的所有不想干的空白符号删除而去。
比如连续的多个空格、回车换行、tab等其实只要有一个空格替代就可以了,然后再在必要的地方插入一个分号之类的。
通过这样的压缩算法后,可以让JS的文件体积大大缩小,从而达到节省带宽的目的。否则,一个JQuery的代码库可是好几十K,这个浪费换作在几年前特别是10十年前那可是很大的。
这些常用的压缩,对于一个网站,已经够用了。要是真正的压缩,会把变量啊之类的都边成abcd这样简单的字符,一样删除空白 回车之类的。