JavaScript operation URL function modified version _javascript tips

Source: Internet
Author: User
Summed up a number of methods to compress it into a function object, only dozens of lines of code, the use of the method is simple and straightforward
Save As Ojburl.js
Copy Code code as follows:

/*
Description: This code can be freely copied and used, but please keep the author information!
Author:kevin website:http://iulog.com/qq:251378427
JS operation URL function use instructions:
Initialization of Var myurl=new objurl (); You can also customize Url:var myurl=new objurl (' http://www.jb51.net ');
Read URL parameter value var val=myurl.get (' abc '); Reading the value of the parameter ABC
Sets the URL parameter myurl.set ("ARG", data); Add/Modify the value of an arg parameter is data
Removes the URL parameter myurl.remove ("Arg"); Remove arg parameter
Gets the processed URL myurl.url ();//generally executes the jump Location.href=myurl.url () directly;
Debugging interface: Myurl.debug (); Modify the function for debugging
*/
function Objurl (URL) {
var ourl=url| | Window.location.href;
var href= ""; the front section
var Params={};//url Parameter Object
var jing= "";//#及后面部分
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<parts.length;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 (+ + "=" +params[k]);
}
}
if (objps.length>0) {
strurl+= "?" +objps.join ("&");
}
if (jing.length>0) {
strurl+=jing;
}
return strurl;
};
This.debug=function () {
The following debug code freedom settings
var objps=[];
For (var k in params) {
Objps.push (+ + "=" +params[k]);
}
alert (OBJPS);//output params all values
};
Init ();
}

Call method
JS operation URL function use instructions:
[Code]
var myurl=new objurl (); Class You can also customize Url:var myurl=new objurl (' http://www.jb51.net ');
var val=myurl.get (' abc '); Reading the value of the parameter ABC
Myurl.set ("Arg", data); Add/Modify the value of an arg parameter is data
Myurl.remove ("Arg"); Remove arg parameter
Myurl.url ();//Get the URL after processing, generally direct execution of the Jump: Location.href=myurl.url ();
Myurl.debug (); This is the debugging interface. Modify the function method to debug
Related Article

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.