国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 龙井市| 湖北省| 基隆市| 鄂伦春自治旗| 洛川县| 六枝特区| 甘洛县| 营口市| 南华县| 琼结县| 株洲市| 瑞金市| 荔浦县| 加查县| 武鸣县| 开封市| 涟水县| 博野县| 怀柔区| 贞丰县| 海盐县| 资源县| 碌曲县| 哈尔滨市| 天峻县| 淅川县| 扶风县| 江口县| 新河县| 津南区| 财经| 河曲县| 云阳县| 宝鸡市| 海兴县| 丁青县| 望都县| 和顺县| 湖口县| 江陵县| 开封县|