js url参数操作插件

// 当前页面url参数操作插件
// Author:Bean
// Date:2014/09/17
;
(function(window, document) {

var UrlParas = function(url) {
return UrlParas.fn.init(url);
}
UrlParas.VERSION = ‘1.0.0‘;
UrlParas.fn = UrlParas.prototype = {

url: "",
pathname: "",
paras: "",
init: function(url) {
this.url = url;
this.pathname = url.split("?")[0];
this.paras = this.get();
return this;
},

//以object类型返回url参数及其取值
get: function(option) {
var paraStr, paras,
url = this.url;
if (url) {
paraStr = url.split("?")[1];
if (paraStr) {
paras = {};
paraStr = paraStr.split("&");
for (var n in paraStr) {
var name = paraStr[n].split("=")[0];
var value = paraStr[n].split("=")[1];
paras[name] = value;
}
} else {
return {};
}
if (!option) {
return paras;
} else {
return paras[option] ? paras[option] : "";
}

}
},

//重设url参数取值,若无此参数则进行创建,若参数赋值为null则进行删除
set: function(option) {
var i, name, val;
if (arguments.length == 2) {
name = arguments[0];
val = arguments[1];
option = {
name: val
};
}
if ("string" === typeof option) {
this.paras[option] = "";
} else if ("object" === typeof option) {
for (i in option) {
if (option[i] === null) {
delete this.paras[i];
} else {
this.paras[i] = option[i];
}
}
} else {

}
return this.build();
},

//删除url中指定参数返回新url
remove: function(option) {
var i;
if ("string" === typeof option) {
option = option.split(",");
for (i in option) {
delete this.paras[option[i]]
}

}
return this.build();
},

//根据url和处理过的paras重新构件url
build: function() {
var i,
newUrl = this.pathname + "?";

for (i in this.paras) {
newUrl += (i + "=" + this.paras[i] + "&");
}

return newUrl.substr(0, newUrl.length - 1);
}

}

UrlParas.fn.init.prototype = UrlParas.fn;

window.urlParas = UrlParas;

})(window, document);

//用法示例
testUrl = "http://127.0.0.1/test/urlParas.html?lang=2&tt=22";
console.log(urlParas(testUrl).get());
console.log(urlParas(testUrl).set("test"));
console.log(urlParas(testUrl).set("test", "bean"));
console.log(urlParas(testUrl).set({
"ajax": "ok",
"lang": null,
"test": null
}));
console.log(urlParas(testUrl).set({
zcsdf: "zcsdf",
lang: "zh-cn"
}));
console.log(urlParas(testUrl).remove("lang,tt"));
console.log(urlParas(testUrl).pathname);

// 输出结果
// Object {lang: "2", tt: "22"}
// http://127.0.0.1/test/urlParas.html?lang=2&tt=22&test=
// http://127.0.0.1/test/urlParas.html?lang=2&tt=22&name=bean
// http://127.0.0.1/test/urlParas.html?tt=22&ajax=ok
// http://127.0.0.1/test/urlParas.html?lang=zh-cn&tt=22&zcsdf=zcsdf
// http://127.0.0.1/test/urlParas.html
// http://127.0.0.1/test/urlParas.html

时间: 2024-10-05 04:55:01

js url参数操作插件的相关文章

js url参数的获取和设置以及删除

//获取url参数的值:name是参数名 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) { return (r[2]); } return null;

js url参数解析获取

function get_url_parm_list(str){ var params=str.substr(str.indexOf('?')+1); var param_list=[]; while(params.indexOf('=')!=-1){ var ind=params.indexOf('='); var k=params.substr(0,ind); var sp=params.indexOf('&'); if(sp==-1){ var v=params.substr(ind+1)

jQuery 插件 获取URL参数

jQuery 获取URL参数的插件 jQuery Url Query String 下载地址:http://plugins.jquery.com/getUrlQueryString.js/ var url="www.abc.com/index.html?key=hello"; var $arrUrl = $.getUrlQueryString(url); var key=$arrUrl["key"];

总结:客户端与服务器端使用正则增加URL参数的方法

先说服务器端的: C#版本 #region URL参数操作 /// <summary> /// URL参数操作 /// </summary> public class UrlPageHelper { /// <summary> /// 以正则表达式方式增加URL参数 /// </summary> /// <param name="key"></param> /// <param name="valu

JS 操作Url参数

项目中需要JS操作路径参数,小弟不才,不跳会,因此查了一下,总结一下使用方法 <script type="text/javascript"> //运用 window.onload = function () { var Request = new Object(); Request = GetRequest(); var urlPara = Request['isChecked']; if (urlPara != null && urlPara != &qu

纯JS设置首页,加入收藏,获取URL参数,解决中文乱码

雪影工作室版权所有,转载请注明[http://blog.csdn.net/lina791211] 1.前言 纯Javascript 设置首页,加入收藏. 2.设置首页 // 设置为主页 function SetHome(obj, vrl) { try { obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl); } catch (e) { if (window.netscape) { try { netscape.s

js获取url参数

   //获取url参数    function getRequest() {         var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object();         if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 

JS获取url参数及url编码、解码

完整的URL由这几个部分构成:scheme://host:port/path?query#fragment ,各部分的取法如下: window.location.href:获取完整url的方法:,即scheme://host:port/path?query#fragment window.location.protocol:获取rul协议scheme window.location.host:获取host window.location.port:获取端口号 window.location.pa

JS对象转URL参数(原生JS和jQuery两种方式)

转自:点击打开链接 现在的js框架将ajax请求封装得非常简单,例如下面: [javascript] view plain copy $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }).done(function( msg ) { alert( "Data Saved: " + msg )