簡書原文git
這個類的主要目的是爲了方便平時編碼中的Url類型的數據操做github
Githubweb
全局名稱是由源碼的最後一行代碼肯定的,默認爲Url,如存在相同名稱的對象會拋出異常;
能夠經過 requirejs的define
獲取json
(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");
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);
結果:api
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.encoded(params)
相反,將URL參數字符串轉爲js對象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, " "));
結果:(從URL參數轉爲js對象會丟失參數類型,所有變爲string)數組
{ "a": { "one": "1", "two": "2", "three": "3" }, "b": [ "1", "2", "3" ] }
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
帶參數的狀況下,默認url2的參數覆蓋url1的參數;
若是但願保留url1的參數能夠將url2的參數寫作ur2="path?&name=value"
,在?
與name間插入一個&
符號;
若是url2與url1參數相同會將參數改成數組;dom
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不存在錨記時,保留url1的錨記,不然url2的錨記覆蓋url1的錨記;
url2結尾爲#
號時,直接清除url1的全部錨記requirejs
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
也能夠傳多個參數post
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; };
不管是否經過new
關鍵字調用都會返回一個實例;
不提供url
參數時,取window.location.href
的值;ui
var token = new Object(); function Url(url) { if (arguments[1] !== token) { return new Url(url, token); } url = trim(url || window.location.href); ... ... }
http://
,https://
也能夠是//
var url = new Url("http://baidu.com"); console.log(url.scheme); // http:// url.scheme = "https://" console.log(url.toString()); // https://baidu.com
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/
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
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
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
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