A very good Javascript operation URL function can be added, modified, and deleted. Some methods are summarized to compress it into a function object. There are only dozens of lines of code. The method is simple and clear.
Save as ojbUrl. js
The Code is as follows:
/*
Note: This code can be freely copied and modified, but please keep the author's information!
Author: Kevin WebSite: http://iulog.com/QQ: 251378427
Instructions for using js url functions:
Initialize var myurl = new objURL (); // You can also customize the URL: var myurl = new objURL ('HTTP: // www.jb51.net ');
Read the url parameter value var val = myurl. get ('abc'); // read the value of the parameter abc
Set the url parameter myurl. set ("arg", data); // Add/modify the value of an arg parameter to data
Remove the url parameter myurl. remove ("arg"); // remove the arg Parameter
Obtain the processed URL myurl. url (); // you can directly jump to location. href = myurl. url ();
Debug interface: myurl. debug (); // modify the function for debugging
*/
Function objURL (url ){
Var ourl = url | window. location. href;
Var href = "";//? Previous section
Var params ={}; // url parameter object
Var jing = ""; // # and subsequent sections
Var init = function (){
Var str = ourl;
Var index = str. indexOf ("#");
If (index> 0 ){
Jing = str. substr (index );
Str = str. substring (0, index );
}
Index = str. indexOf ("? ");
If (index> 0 ){
Href = str. substring (0, index );
Str = str. substr (index + 1 );
Var parts = str. split ("&");
For (var I = 0; I Var kv = parts [I]. split ("= ");
Params [kv [0] = kv [1];
}
} Else {
Href = ourl;
Params = {};
}
};
This. set = function (key, val ){
Params [key] = encodeURIComponent (val );
};
This. remove = function (key ){
If (key in params) params [key] = undefined;
};
This. get = function (key ){
Return params [key];
};
This. url = function (key ){
Var strurl = href;
Var objps = [];
For (var k in params ){
If (params [k]) {
Objps. push (k + "=" + params [k]);
}
}
If (objps. length> 0 ){
Strurl + = "? "+ Objps. join ("&");
}
If (jing. length> 0 ){
Strurl + = jing;
}
Return strurl;
};
This. debug = function (){
// The following debugging code can be set freely:
Var objps = [];
For (var k in params ){
Objps. push (k + "=" + params [k]);
}
Alert (objps); // output all params values
};
Init ();
}
Call Method
Instructions for using js url functions:
[Code]
Var myurl = new objURL (); // initialization. You can also customize the URL: var myurl = new objURL ('HTTP: // www.jb51.net '');
Var val = myurl. get ('abc'); // read the value of parameter abc
Myurl. set ("arg", data); // Add/modify the value of an arg parameter as data
Myurl. remove ("arg"); // remove the arg Parameter
Myurl. url (); // obtain the processed URL. Generally, the jump is directly executed: location. href = myurl. url ();
Myurl. debug (); // This is the debugging interface. Modify the Function Method for debugging