没有哈希的javascript窗口位置href?
我有:
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() ?
I have:
var uri = window.location.href;
That provides http://example.com/something#hash
What's the best and easiest way to get the entire path without the #hash
?
uri = http://example.com/something#hash
nohash = http://example.com/something
I tried using location.origin+location.pathname
which doesn't work in every browser. I tried using location.protocol+'//'+location.host+location.pathname
which looks like kind of a crappy solution to me.
What is the best and easiest way to do so? maybe I query for location.hash and try to substr() this from the uri?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您不关心端口号或查询字符串,则
location.protocol+'//'+location.host+location.pathname
是正确的语法如果您确实关心:
https://developer.mozilla.org/en/DOM/window.location
或者
您也可以执行 <代码>location.href.replace(location.hash,"")
它将删除从第一个 # 开始的所有内容,无论字符串中的其他哈希字符如何
。或者创建一个 URL 对象:
location.protocol+'//'+location.host+location.pathname
is the correct syntax if you do not care about port number or querystringIf you do care:
https://developer.mozilla.org/en/DOM/window.location
or
You can also just do a
location.href.replace(location.hash,"")
It will remove EVERYTHING from the FIRST # and on regardless of other hash characters in the string
Alternatively create a URL object:
万能的方式也是越小越好吗?
Is the universal way also the smaller?
较短的解决方案:
没有查询字符串和哈希
location.href.split(location.search||location.hash||/[?#]/)[0]
仅无哈希
location.href.split (location.hash||"#")[0]
(我一般用第一个)
Shorter solutions:
without query string and hash
location.href.split(location.search||location.hash||/[?#]/)[0]
only without hash
location.href.split(location.hash||"#")[0]
(I usually use the first one)
我一直在寻找这个答案:
I was looking for this answer:
我喜欢让本机代码尽可能地完成繁重的工作。我开始认为浏览器可以比我们做出最佳猜测更好地识别 URL 的各个部分。所以这里有另一种变体供您考虑:
我想这需要一点解释:正在发生的事情是我们正在构建一个 URL 对象,其中当前 URL 上有一个空哈希作为基础,即如果有一个哈希,它会被一个空替换一。然后,我们从隐式转换字符串(URL 对象的 href)中删除最后一个字符(“#”)。
I'm a fan of letting native code do the heavy lifting as much as possible. I'd come to think that a browser can recognize the parts of URLs better than us doing a best guess. So here's another variant for your consideration:
I guess this needs a litte explanation: What's happening is we're constructing a URL object with an empty hash on the current URL as base, i.e. if there is a hash, it gets replace by an empty one. We then remove the last character ('#') from the implicitly cast string (href of the URL object).
我更喜欢这种简短的单行代码,它直接返回想要的结果,而不是创建一个数组。
仅删除哈希 (#hash):
同时删除哈希 (#hash) 和查询参数 (?query=value&...):
这是正则表达式 (
/.../
) 用于从第一个替换 ? (\?
) 或 ((...|...)
) # (#
),所有字符 (.*
) 直到字符串末尾 ($
),由空字符串 (''
) 组成。I prefer this short one-liners which directly return the wanted result instead of creating an array.
To remove only the hash (#hash):
To remove both the hash (#hash) and the query params (?query=value&...):
This is regex (
/.../
) for replacing from the first ? (\?
) or ((...|...)
) # (#
), all characters (.*
) until the end of the string ($
), by an empty string (''
).ES2020:
ES2020: