JavaScript - 获取URL路径的一部分

时间:2011-08-04 16:04:35

标签: javascript jquery url

使用JavaScript从URL中提取路径的正确方法是什么?

例:
我有URL
http://www.somedomain.com/account/search?filter=a#top
但是我想得到这个部分 /帐户/搜索

如果有任何可以利用的东西,我正在使用jQuery。

6 个答案:

答案 0 :(得分:365)

内置window.location对象的属性将为当前窗口提供该属性。

// If URL is http://www.somedomain.com/account/search?filter=a#top

window.location.pathname // /account/search

// For reference:

window.location.host     // www.somedomain.com (includes port if there is one)
window.location.hostname // www.somedomain.com
window.location.hash     // #top
window.location.href     // http://www.somedomain.com/account/search?filter=a#top
window.location.port     // (empty string)
window.location.protocol // http:
window.location.search   // ?filter=a  



更新,对任何URL使用相同的属性:

事实证明,这个模式正在被标准化为一个名为URLUtils的接口,猜猜是什么?现有的window.location对象和锚元素都实现了界面。

因此,您可以为任何网址使用上述相同的属性 - 只需使用网址创建一个锚点并访问属性:

var el = document.createElement('a');
el.href = "http://www.somedomain.com/account/search?filter=a#top";

el.host        // www.somedomain.com (includes port if there is one[1])
el.hostname    // www.somedomain.com
el.hash        // #top
el.href        // http://www.somedomain.com/account/search?filter=a#top
el.pathname    // /account/search
el.port        // (port if there is one[1])
el.protocol    // http:
el.search      // ?filter=a

[1]:对包含端口的属性的浏览器支持不一致,请参阅:http://jessepollak.me/chrome-was-wrong-ie-was-right

适用于最新版本的Chrome和Firefox 。我没有要测试的Internet Explorer版本,所以请使用JSFiddle示例测试自己。

JSFiddle example

还有一个即将到来的URL对象,它将为URL本身提供此支持,而不使用anchor元素。看起来目前没有稳定的浏览器支持它,但据说它将在Firefox 26中使用。When you think you might have support for it, try it out here

答案 1 :(得分:44)

window.location.href.split('/');

将为您提供一个包含所有URL部分的数组,您可以像普通数组一样访问它。

或@Dylan建议的更优雅的解决方案,只有路径部分:

window.location.pathname.split('/');

答案 2 :(得分:27)

如果这是当前网址,请使用 window.location.pathname ,否则请使用此正则表达式:

var reg = /.+?\:\/\/.+?(\/.+?)(?:#|\?|$)/;
var pathname = reg.exec( 'http://www.somedomain.com/account/search?filter=a#top' )[1];

答案 3 :(得分:7)

有一个有用的Web API方法,称为URL

const url = new URL('http://www.somedomain.com/account/search?filter=a#top');
console.log(url.pathname.split('/'));
const params = new URLSearchParams(url.search)
console.log(params.get("filter"))

答案 4 :(得分:2)

如果您有一个抽象的URL字符串(不是来自当前window.location的字符串),则可以使用以下技巧:

let yourUrlString = "http://example.com:3000/pathname/?search=test#hash";

let parser = document.createElement('a');
parser.href = yourUrlString;

parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"

感谢jlong

答案 5 :(得分:0)

如果您想获取已存储在变量中的URL的一部分,我建议URL-Parse

const Url = require('url-parse');
const url = new Url('https://github.com/foo/bar');

根据文档,它提取了以下部分:

  

返回的url实例包含以下属性:

     

协议:URL的协议方案(例如http :)。   斜杠:布尔值,指示协议后是否跟随两个正斜杠(//)。   auth:身份验证信息部分(例如,username:password)。   username:基本认证的用户名。   password:基本认证密码。   host:带有端口号的主机名。   hostname:不带端口号的主机名。   port:可选端口号。   pathname:URL路径。   查询:包含查询字符串的已解析对象,除非将解析设置为false。   哈希:URL的“片段”部分,包括井号(#)。   href:完整的URL。   origin:URL的来源。

相关问题