JavaScript 功能类 Url.js

时间:2023-12-30 23:28:38

简书原文

这个类的主要目的是为了方便平时编码中的Url类型的数据操作

Github

全局名称

全局名称是由源码的最后一行代码确定的,默认为Url,如存在相同名称的对象会抛出异常;

可以通过 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");

静态方法

  • 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);

结果:

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参数相同会将参数改为数组;

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的所有锚记

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

也可以传多个参数

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的值;

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