【发布时间】:2011-08-14 15:59:40
【问题描述】:
我有:
var uri = window.location.href;
提供http://example.com/something#hash
在没有#hash 的情况下获得整个路径的最佳和最简单的方法是什么?
uri = http://example.com/something#hash
nohash = http://example.com/something
我尝试使用location.origin+location.pathname,但它不适用于所有浏览器。我尝试使用location.protocol+'//'+location.host+location.pathname,这对我来说似乎是一种糟糕的解决方案。
最好和最简单的方法是什么?也许我查询 location.hash 并尝试从 uri 中 substr() 这个?
【问题讨论】:
-
顺便说一句,如果您这样做只是为了链接到同一页面上的
#section,只需将链接 href 设置为#section。您不需要获取页面的基本 url,然后在最后连接哈希。
标签: javascript location substring