這個(gè)類的主要目的是為了方便平時(shí)編碼中的Url類型的數(shù)據(jù)操作
全局名稱
全局名稱是由源碼的最后一行代碼確定的谜洽,默認(rèn)為Url,如存在相同名稱的對(duì)象會(huì)拋出異常蛛勉;
可以通過 requirejs的define
獲取
(function (window, name) {
if (name in window) {
throw new Error(["already '", name, "' in 'window'"].join(""));
}
...
window[name] = Url;
if (typeof window.define === "function") {
window.define(name, [], function () { return Url; });
}
})(window, "Url");
靜態(tài)方法
-
Url.encoded(params)
將對(duì)象編碼為URL參數(shù)痪枫,類似于jQuery.param()
喊巍,不包含“搀继?”;
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [1,2,3]
};
var recursiveEncoded = Url.encoded(myObject);
var recursiveDecoded = decodeURIComponent(recursiveEncoded);
console.log(recursiveEncoded);
console.log(recursiveDecoded);
結(jié)果:
a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B1%5D=2&b%5B2%5D=3
a[one]=1&a[two]=2&a[three]=3&b[]=1&b[1]=2&b[2]=3
-
Url.parseSearch(search)
與Url.encoded(params)
相反憾筏,將URL參數(shù)字符串轉(zhuǎn)為js對(duì)象
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [1,2,3]
};
var recursiveEncoded = Url.encoded(myObject);
var obj = Url.parseSearch(recursiveEncoded);
console.log(JSON.stringify(obj, null, " "));
結(jié)果:(從URL參數(shù)轉(zhuǎn)為js對(duì)象會(huì)丟失參數(shù)類型任洞,全部變?yōu)閟tring)
{
"a": {
"one": "1",
"two": "2",
"three": "3"
},
"b": [
"1",
"2",
"3"
]
}
-
Url.combine(url1, url2)
將2個(gè)url組合成一個(gè)新的Url
Url.combine("/","/api/user/get").toString(); // /api/user/get
Url.combine("/http/web","/api/user/get").toString(); // /api/user/get
Url.combine("/http/web","api/user/get").toString(); // /api/user/get
Url.combine("/http/web/","api/user/get").toString(); // /http/api/user/get
Url.combine("/http/web/","../api/user/get").toString(); // /http/api/user/get
Url.combine("/http/web","../api/user/get").toString(); // /api/user/get
Url.combine("/http/web","./api/user/get").toString(); // /http/web/api/user/get
帶參數(shù)的情況下蓄喇,默認(rèn)url2的參數(shù)覆蓋url1的參數(shù);
如果希望保留url1的參數(shù)可以將url2的參數(shù)寫做ur2="path?&name=value"
交掏,在?
與name間插入一個(gè)&
符號(hào)妆偏;
如果url2與url1參數(shù)相同會(huì)將參數(shù)改為數(shù)組;
Url.combine("/http/web?id=1","api/user").toString(); // //http/api/user
Url.combine("/http/web?id=1","?name=2").toString(); // /http/web?name=2
Url.combine("/http/web?id=1","?&name=2").toString(); // /http/web?id=1&name=2
Url.combine("/http/web?id=1","?&id=2").toString(); // /http/web?id%5B%5D=1&id%5B1%5D=2
Url.combine("/http/web?id=1","./../?id=2").toString(); // /http/?id=2
Url.combine("/http/web?id=1","./../?&name=2").toString(); // "/http/?id=1&name=2"
url2不存在錨記時(shí)盅弛,保留url1的錨記钱骂,否則url2的錨記覆蓋url1的錨記;
url2結(jié)尾為#
號(hào)時(shí)熊尉,直接清除url1的所有錨記
Url.combine("/http/web?id=1#h1","api/user").toString(); ///http/api/user#h1
Url.combine("/http/web?id=1#h1","./../?&name=2#h2").toString(); // /http/?id=1&name=2#h2
Url.combine("/http/web?id=1#h1","api/user#").toString(); // /http/api/user?id=1
也可以傳多個(gè)參數(shù)
function combine(url1, url2) {
... ...
}
Url.combine = function (url1, url2) {
if (arguments.length < 2) {
return arguments[0];
}
var _base = url1;
for (var i = 1; i < arguments.length; i++) {
_base = combine(_base, arguments[i]).toString();
}
return _base;
};
實(shí)例化
無論是否通過new
關(guān)鍵字調(diào)用都會(huì)返回一個(gè)實(shí)例罐柳;
不提供url
參數(shù)時(shí),取window.location.href
的值狰住;
var token = new Object();
function Url(url) {
if (arguments[1] !== token) {
return new Url(url, token);
}
url = trim(url || window.location.href);
... ...
}
實(shí)例屬性
-
scheme
url協(xié)議類型张吉,如http://
,https://
也可以是//
var url = new Url("http://baidu.com");
console.log(url.scheme); // http://
url.scheme = "https://"
console.log(url.toString()); // https://baidu.com
-
domain
url的域名部分
var url = new Url("http://baidu.com/api/");
console.log(url.domain); // baidu.com
url.domain= "google.com"
console.log(url.toString()); // http://google.com/api/
-
path
url的路徑部分
var url = new Url("http://baidu.com/api/get?id=1");
console.log(url.path); // /api/get
url.path = "api/post"
console.log(url.toString()); // http://baidu.com/api/post?id=1
-
query
url的參數(shù)部分
var url = new Url("http://baidu.com/api/get?id=1#title");
console.log(url.query); // id=1
url.query = "name=1&sex=男"
console.log(url.toString()); // http://baidu.com/api/get?name=1&sex=%E7%94%B7#title
url.query = ""
console.log(url.toString()); // http://baidu.com/api/get#title
-
params
url的參數(shù)部分被解釋后的實(shí)體對(duì)象
var url = new Url("http://baidu.com/api/get?id=1#title");
console.log(url.params.id); // 1
url.params.id = 2;
url.params.name = "blqw";
console.log(url.toString()); // http://baidu.com/api/get?id=2&name=blqw#title
-
anchor
url的錨記部分
var url = new Url("http://baidu.com/api/get?id=1#title");
console.log(url.anchor); // #title
url.anchor = "content";
console.log(url.toString()); // http://baidu.com/api/get?id=1#content
url.anchor = "";
console.log(url.toString()); // http://baidu.com/api/get?id=1