Answers:
这是可能的,但是您必须从location
对象手动构建它:
location.protocol + '//' + location.host + location.pathname
'//' + location.host + location.pathname
http://www.example.com:8080/asdf.html?foo=bar
location.origin
,我相信这也可以解决@izogfif的问题。
每个答案都相当复杂。这里:
var url = window.location.href.split('?')[0];
即使一个?不存在,它将仍然返回第一个参数,它将是您的完整URL减去查询字符串。
它也与协议无关,这意味着您甚至可以将其用于ftp,itunes.etc等内容。
window.location.href.split(/[?#]/)[0];
var url = "tp://mysite.com/somedir/somefile/?foo=bar&loo=goo"
url.substring(0,url.indexOf("?"));
url.split('?')[0]
如果您查看文档,则可以仅从window
对象中获取您感兴趣的属性,即
protocol + '//' + hostname + pathname