我正在使用jQuery。如何获取当前URL的路径并将其分配给变量?

示例URL:

http://localhost/menuname.de?foo=bar&number=0

当前回答

这也将起作用:

var currentURL = window.location.href;

其他回答

window.location将为您提供当前URL,您可以从中提取任何您想要的内容。。。

 var currenturl = jQuery(location).attr('href');

在纯jQuery样式中:

$(location).attr('href');

location对象还具有其他财产,如host、hash、protocol和pathname。

请参阅purl.js。这将非常有用,也可以使用,具体取决于jQuery。这样使用:

$.url().param("yourparam");

最短的方法(11个字符)是

让myUrl=“”+位置console.log(myUrl);