Self-written jquery extension Functions

Source: Internet
Author: User

; (Function ($ ){

$. Request = {};
$. Validate = {};
$. Common = {};

$. Extend (jquery. Request ,{
Querystring: function (name ){
VaR url = Document. url;
VaR arr = URL. Split ('? ');
If (ARR. Length <2 ){
Return '';
}
Url = arr [1];

// Remove the last # character
If (URL. lastindexof ('#') = (URL. Length-1 ))
Url = URL. substring (0, URL. Length-1 );

VaR arrquerystringpair = URL. Split ('&');
If (arrquerystringpair. Length = 0)
Return '';

// The split method is not used because if the backurl is like ahr0cdnwed9zdgf0dxm9mw =
// The value after the equal sign cannot be obtained correctly using the split method.
For (VAR I = 0; I <arrquerystringpair. length; ++ I ){
VaR startindex = arrquerystringpair [I]. indexof ('=') + 1;
VaR sname = arrquerystringpair [I]. substr (0, startindex-1 );
VaR result = arrquerystringpair [I]. substr (startindex, arrquerystringpair [I]. Length-startindex );
If (sname. tolowercase () = Name. tolowercase ()){
Return result;
}
}

Return '';
},


/// Call example
/// Var Params = [];
/// Var o = new object ();
/// O. Name = '_';
/// O. value = '_';
/// Params. Push (O );

/// O = new object ();
/// O. Name = 'status ';
/// O. value = '2 ';
/// Params. Push (O );

/// Var url = $. Request. replacequerystring (Params );
///
Replacequerystring: function (arrparams ){
VaR url = Document. url;
VaR desturl = NULL;
VaR arr = URL. Split ('? ');
If (ARR. Length <2 ){
Desturl = arr [0] + '? ';
For (VAR I = 0; I <arrparams. length; ++ I ){
Desturl + = '&' + arrparams [I]. Name + '=' + arrparams [I]. value;
}
Return desturl;
}

If (ARR [1]. indexof ('&') = 0)
Arr [1] = arr [1]. substring (1, arr [1]. Length );
Url = arr [1];

Desturl = arr [0];

If (URL! = ''){
// Remove the last # character
If (URL. lastindexof ('#') = (URL. Length-1 ))
Url = URL. substring (0, URL. Length-1 );

VaR arrquerystringpair = URL. Split ('&');
If (arrquerystringpair. Length = 0)
Return desturl;

Desturl + = '? ';

If (arrquerystringpair. length> 0 & (arrquerystringpair [0]! = '')){
For (var j = 0; j <arrquerystringpair. length; ++ J ){
VaR arrtemp = arrquerystringpair [J]. Split ('= ');
If (arrtemp. Length = 1) continue;
If (arrtemp [0] = '') continue;

VaR flag = false;
For (var k = 0; k <arrparams. length; ++ K ){
If (arrparams [K]. Name. tolowercase () = arrtemp [0]. tolowercase ()){
Desturl + = '&' + arrparams [K]. Name + '=' + arrparams [K]. value;
Arrparams [K]. Added = true;
Flag = true;
Break;
}
}

If (flag = false)
Desturl + = '&' + arrtemp [0] + '=' + arrtemp [1];
}

For (var k = 0; k <arrparams. length; ++ K ){
If (! Arrparams [K]. Added ){
Desturl + = '&' + arrparams [K]. Name + '=' + arrparams [K]. value;
Arrparams [K]. Added = true;
}
}
}
} Else {
Desturl + = '? ';
For (var k = 0; k <arrparams. length; ++ K ){
If (K! = 0)
Desturl + = '&';
Desturl + = arrparams [K]. Name + '=' + arrparams [K]. value;
}
}

Return desturl;
}
});

$. Extend (jquery. Validate ,{
Isemail: function (email ){
Return/^. + @. + \ .. {2, 3} $/g. Test (email );
},
Isint: function (STR ){
Return/^ \ D + $/IMG. Test (STR );
}
});

$. Extend (jquery. Common ,{

// Delete the value from a comma-separated string
// For example, delete C from A, B, C, and D to get a, B, d
Removefromcommajoinedtext: function (value, container ){
If (value. Length = 0)
Return '';

// Remove commas (,)
Value = value. Replace (/^,/, ''). Replace (/, $ /,'');
Container = container. Replace (/^,/, ''). Replace (/, $ /,'');

If (Container = value)
{
Return '';
}

VaR sarray = container. Split (',');
For (VAR I = sarray. Length-1; I> = 0; -- I)
{
If (sarray [I] = value)
Sarray [I] = undefined;
}

VaR result = sarray. Join (',');
// Because undefined is connected to, replace,
Result = result. Replace (/,,/,',');
Result = result. Replace (/^,/, ''). Replace (/, $ /,'');

Return result;
},


/* Determine whether two JS functions have equal objects
The final comparison result is true if the two object attributes are in different order but with the same number and value during initialization.
Supports other types of attributes, such as objects, arrays, numbers, and strings.

VaR A = {name: "yuanxp", ID: 9, go: {A: '1', B: '2 '}};
VaR B = {ID: 9, name: "yuanxp", 'Go': {A: '1', B: '2 '}};

VaR r = $. Common. Equal (A, B );
Alert (R );
 
*/
Equal: function (obja, objb ){
If (typeof arguments [0]! = Typeof arguments [1])
Return false;

// Array
If (arguments [0] instanceof array)
{
If (arguments [0]. length! = Arguments [1]. length)
Return false;

VaR allelementsequal = true;
For (VAR I = 0; I <arguments [0]. length; ++ I)
{
If (typeof arguments [0] [I]! = Typeof arguments [1] [I])
Return false;

If (typeof arguments [0] [I] = 'number' & typeof arguments [1] [I] = 'number ')
Allelementsequal = (arguments [0] [I] = arguments [1] [I]);
Else
Allelementsequal = arguments. callee (arguments [0] [I], arguments [1] [I]); // recursively determines whether an object is equal
}
Return allelementsequal;
}

// Object
If (arguments [0] instanceof object & arguments [1] instanceof object)
{
VaR result = true;
VaR attributelengtha = 0, attributelengthb = 0;
For (VAR o in arguments [0])
{
// Determine whether the attributes of the two objects with the same name are the same (number or string)
If (typeof arguments [0] [O] = 'number' | typeof arguments [0] [O] = 'string ')
Result = eval ("arguments [0] ['" + O + "'] = arguments [1] ['" + O + "']");
Else {
// If the object's attributes are also objects, recursively determine the attributes of the same name of the two objects
// If (! Arguments. callee (arguments [0] [O], arguments [1] [O])
If (! Arguments. callee (eval ("arguments [0] ['" + O + "']"), eval ("arguments [1] ['" + O + "']")
{
Result = false;
Return result;
}
}
++ Attributelengtha;
}

For (VAR o in arguments [1]) {
++ Attributelengthb;
}

// If the attribute numbers of two objects are not the same, the two objects are not the same.
If (attributelengtha! = Attributelengthb)
Result = false;
Return result;
}
Return arguments [0] = arguments [1];

}
});

}) (Jquery );

// Merge the elements of two Arrays
Array. Prototype. Union = function (arrayb ){
For (VAR I = 0; I <this. length; ++ I ){
VaR inarrid = false;
For (var j = 0; j <arrayb. length; ++ J ){
If (arrayb [J] = This [I]) {
Inarrid = true;
Break;
}
}

If (! Inarrid ){
Arrayb. Push (this [I]);
}
}

VaR result = arrayb. Join (',');
Result = result. Replace (/^,/, ''). Replace (/, $ /,'');
Return result;
}

Array. Prototype. removeindex = function (I)
{
If (isnan (I) | I> This. length)
Return false;
This. splice (I, 1 );
}
 
Array. Prototype. Remove = function (key)
{
For (VAR I = 0; I <this. length; ++ I)
{
If (this [I] = key)
This. splice (I, 1 );
}
}

 
/*
B = ['1', '2', '3', '4', '5'];
Alert ("elements:" + B + "nlength:" + B. Length );
B. Remove ('4'); // delete an element with a value of '4'
B. removeindex (3) // Delete the element whose subscript is 1
Alert ("elements:" + B + "nlength:" + B. Length );
*/

// Determine whether the array contains a JS function of an element
Array. Prototype. Contains = function (OBJ ){
VaR I = This. length;
While (I --){
If (this [I] === OBJ ){
Return true;
}
}
Return false;
}

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.