Several Methods for fix-ie5.js extension not available in IE5 _ javascript tips

Source: Internet
Author: User
Tags stringreplace
Fix-ie5.js extensions in IE5 cannot use several methods in IE5 Javascript:
Array does not support push (), pop (); Function does not support apply (); The String object's replace method does not support replacement into a processing Function.
Use the following code to fix the problem that the above method is invalid in ie5.
/*
Fix-ie5.js, version 1.0 (pre-release) () x3
Copyright 2005, Dean Edwards
Web: http://dean.edwards.name/

This software is licensed under the CC-GNU LGPL
Web: http://creativecommons.org/licenses/LGPL/2.1/
*/

If (/MSIE 5.0/. test (navigator. userAgent) new function (){

Var $ apply = function ($ function, $ object, $ arguments ){
$ Function. apply ($ object, $ arguments );
};

// Fix String. replace
If (''. replace (/^/, String )){
// Preserve String. replace
Var _ stringReplace = String. prototype. replace;
// Create String. replace for handling functions
Var _ functionReplace = function ($ expression, $ replacement ){
Var $ match, $ newString = "", $ string = this;
While ($ string & ($ match = extends expression.exe c ($ string ))){
$ NewString + = $ string. slice (0, $ match. index) + $ apply ($ replacement, this, $ match );
$ String = $ string. slice ($ match. lastIndex );
}
Return $ newString + $ string;
};
// Replace String. replace
String. prototype. replace = function ($ expression, $ replacement ){
This. replace = (typeof $ replacement = "function ")? _ FunctionReplace: _ stringReplace;
Return this. replace ($ expression, $ replacement );
};
}

// Fix Function. apply
If (! Function. apply ){
Var APPLY = "apply-" + Number (new Date );
$ Apply = function (f, o, ){
Var r;
O [APPLY] = f;
Switch (a. length) {// deconstruct for speed
Case 0: r = o [APPLY] (); break;
Case 1: r = o [APPLY] (a [0]); break;
Case 2: r = o [APPLY] (a [0], a [1]); break;
Case 3: r = o [APPLY] (a [0], a [1], a [2]); break;
Case 4: r = o [APPLY] (a [0], a [1], a [2], a [3]); break;
Default:
Var aa = [], I = a. length-1;
Do aa [I] = "a [" + I + "]"; while (I --);
Eval ("r = o [APPLY] (" + aa + ")");
}
Delete o [APPLY];
Return r;
};
// Fix ICommon
ICommon. valueOf. prototype. inherit = function (){
Return $ apply (arguments. callee. caller. ancestor, this, arguments );
};
}

// Array fixes
If (! []. Push) Array. prototype. push = function (){
For (var I = 0; I <arguments. length; I ++ ){
This [this. length] = arguments [I];
}
Return this. length;
};
If (! []. Pop) Array. prototype. pop = function (){
Var $ item = this [this. length-1];
This. length --;
Return $ item;
};
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.