我正在使用jQuery。如何获取当前URL的路径并将其分配给一个变量?
示例URL:
示例URL:
http://localhost/menuname.de?foo=bar&number=0
http://localhost/menuname.de?foo=bar&number=0
$(document).ready(function() {
//jQuery
$(location).attr('href');
//Pure JavaScript
var pathname = window.location.pathname;
// To show it in an alert window
alert(window.location);
});
$.getJSON("idcheck.php?callback=?", { url:$(location).attr('href')}, function(json){
//alert(json.message);
});
在 iframe 中获取父窗口的 URL:
$(window.parent.location).attr('href');
注意:仅适用于相同的域名
如果您想获取根站点的路径,请使用以下代码:
$(location).attr('href').replace($(location).attr('pathname'),'');
.replace('#.*', '')
吗?不仅要删除井号,还要删除井号后面的所有内容? - Jonas Kölker$(location).attr('hostname'); //origin URL
$(location).attr('pathname'); // path name
$(location).attr('hash'); // everything comes after hash
非常常用的前三个如下:
1. window.location.hostname
2. window.location.href
3. window.location.pathname
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
在jstl中,我们可以使用pageContext.request.contextPath
来访问当前URL路径。如果您想要进行AJAX调用,
url = "${pageContext.request.contextPath}" + "/controller/path"
https://dev59.com/6nRC5IYBdhLWcg3wG9To
中,这将给出http://stackoverflow.com/controller/path
。