在IE5下的Javascript:
Array不支持push(),pop();Function不支持apply();String对象的replace方法不支持替换成一个处理函数。
使用下面的代码就可修复上述方法在IE5下无效的问题。
/*
fix-ie5.js,version1.0(pre-release)(2005/05/15)x3
Copyright2005,DeanEdwards
Web:http://dean.edwards.name/
ThissoftwareislicensedundertheCC-GNULGPL
Web:http://creativecommons.org/licenses/LGPL/2.1/
*/
if(/MSIE5.0/.test(navigator.userAgent))newfunction(){
var$$apply=function($function,$object,$arguments){
$function.apply($object,$arguments);
};
//fixString.replace
if(''.replace(/^/,String)){
//preserveString.replace
var_stringReplace=String.prototype.replace;
//createString.replaceforhandlingfunctions
var_functionReplace=function($expression,$replacement){
var$match,$newString="",$string=this;
while($string&&($match=$expression.exec($string))){
$newString+=$string.slice(0,$match.index)+$$apply($replacement,this,$match);
$string=$string.slice($match.lastIndex);
}
return$newString+$string;
};
//replaceString.replace
String.prototype.replace=function($expression,$replacement){
this.replace=(typeof$replacement=="function")?_functionReplace:_stringReplace;
returnthis.replace($expression,$replacement);
};
}
//fixFunction.apply
if(!Function.apply){
varAPPLY="apply-"+Number(newDate);
$$apply=function(f,o,a){
varr;
o[APPLY]=f;
switch(a.length){//deconstructforspeed
case0:r=o[APPLY]();break;
case1:r=o[APPLY](a[0]);break;
case2:r=o[APPLY](a[0],a[1]);break;
case3:r=o[APPLY](a[0],a[1],a[2]);break;
case4:r=o[APPLY](a[0],a[1],a[2],a[3]);break;
default:
varaa=[],i=a.length-1;
doaa[i]="a["+i+"]";while(i--);
eval("r=o[APPLY]("+aa+")");
}
deleteo[APPLY];
returnr;
};
//fixICommon
ICommon.valueOf.prototype.inherit=function(){
return$$apply(arguments.callee.caller.ancestor,this,arguments);
};
}
//arrayfixes
if(![].push)Array.prototype.push=function(){
for(vari=0;i<arguments.length;i++){
this[this.length]=arguments[i];
}
returnthis.length;
};
if(![].pop)Array.prototype.pop=function(){
var$item=this[this.length-1];
this.length--;
return$item;
};
};