国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript parseUrl函數(shù)(來自國外的獲取網(wǎng)址url參數(shù))

2019-11-21 00:25:59
字體:
供稿:網(wǎng)友
函數(shù)代碼
復(fù)制代碼 代碼如下:

function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^/?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(///([^//?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^//])/,'/$1'),
relative: (a.href.match(/tps?:////[^//]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^///,'').split('/')
};
}

用法:
復(fù)制代碼 代碼如下:

var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
myURL.file; // = 'index.html'
myURL.hash; // = 'top'
myURL.host; // = 'abc.com'
myURL.query; // = '?id=255&m=hello'
myURL.params; // = Object = { id: 255, m: hello }
myURL.path; // = '/dir/index.html'
myURL.segments; // = Array = ['dir', 'index.html']
myURL.port; // = '8080'
myURL.protocol; // = 'http'
myURL.source; // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'

演示代碼:

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執(zhí)行]
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五台县| 兰州市| 中江县| 泾川县| 五台县| 开阳县| 成武县| 郎溪县| 淄博市| 长阳| 金乡县| 平潭县| 阆中市| 改则县| 淮滨县| 高淳县| 东宁县| 新和县| 钟山县| 栾城县| 讷河市| 许昌县| 繁昌县| 夹江县| 射洪县| 鄂托克前旗| 沅陵县| 安康市| 广平县| 密山市| 红原县| 榆林市| 宿州市| 丰顺县| 镇江市| 丹阳市| 邢台市| 久治县| 安国市| 确山县| 司法|