JavaScript Action URL function (add, modify, delete URL parameters)

Source: Internet
Author: User

This two days to encounter the operation of the URL js, online Search A lot of methods, are direct operation, use is very inconvenient, there are many duplicate code.

So set hundred home strict, summed up the various methods, it compressed into a function object, only dozens of lines of code, the use of simple method

Ojburl.js

The code is as follows Copy Code


/*
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.111cn.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 is as follows copy code

var myurl= New Objurl (); Class You can also customize Url:var myurl=new objurl (' http://www.111cn.net ');
var val=myurl.get (' abc ');//Read the value of the parameter ABC
Myurl.set ("arg", data);//Add/Modify the value of an arg parameter is Data
Myurl.remove ("Arg"); Remove the arg parameter
Myurl.url ();//Get the processed URL, and generally execute the jump directly: Location.href=myurl.url ();
Myurl.debug ();//This is the Debug interface. Modify the function method to debug

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.