JavaScript 功能類 Url.js

簡書原文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");

靜態方法

  • Url.encoded(params)

    將對象編碼爲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.parseSearch(search)

    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(url1, url2)

    將2個url組合成一個新的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

帶參數的狀況下,默認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);
    ... ...
}

實例屬性

  • scheme

    url協議類型,如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的參數部分
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的參數部分被解釋後的實體對象
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
相關文章
相關標籤/搜索