jquery解析链接

有时候我们需要分析一个url,在使用PHP和node.js时都有非常方便的方法/模块,但在JS我好像没发现什么好的方法,通过查找,在jQuery中发现了一个比较方便的方法。

示例如下:

var url = 'https://liyangweb.com/javascript/312.html?test=123#top';
var a = $('<a>', {
	href: url
});
var sResult = '<b>Protocol:</b> ' + a.prop('protocol') + '<br/>' + '<b>Host name: </b>' + a.prop('hostname') + '<br/>' + '<b>Path: </b>' + a.prop('pathname') + '<br/>' + '<b>Query: </b>' + a.prop('search') + '</br>' + '<b>Hash: </b>' + a.prop('hash');

document.write(sResult);

输出结果为:

Protocol: http:
Host name: www.liyangweb.com
Path: /javascript/312.html
Query: ?test=123
Hash: #top

2 comments:

评论已关闭。