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

首頁 > 開發(fā) > JS > 正文

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

2024-09-06 12:45:49
字體:
供稿:網(wǎng)友
函數(shù)代碼
代碼如下:
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('/')
};
}

用法:
代碼如下:
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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 罗源县| 射洪县| 上杭县| 涞水县| 余姚市| 双流县| 芦山县| 简阳市| 丰台区| 滦平县| 潮安县| 宕昌县| 嘉黎县| 湖北省| 麻阳| 宜春市| 保定市| 积石山| 宝鸡市| 水城县| 丰原市| 西乌珠穆沁旗| 叶城县| 辉县市| 综艺| 保康县| 松桃| 合肥市| 凉城县| 辽阳县| 随州市| 武功县| 武川县| 临城县| 宝应县| 开阳县| 延安市| 安国市| 阿巴嘎旗| 石阡县| 松江区|