如何使用 JavaScript 检查 URL 中的#hash?
我有一些 jQuery/JavaScript 代码,仅当 URL 中存在哈希 (#
) 锚链接时才运行。 如何使用 JavaScript 检查该字符? 我需要一个简单的包罗万象的测试来检测如下 URL:
example.com/page.html#anchor
example.com/page.html#anotheranchor
基本上是这样的的行:
if (thereIsAHashInTheUrl) {
do this;
} else {
do this;
}
I have some jQuery/JavaScript code that I want to run only when there is a hash (#
) anchor link in a URL. How can you check for this character using JavaScript? I need a simple catch-all test that would detect URLs like these:
example.com/page.html#anchor
example.com/page.html#anotheranchor
Basically something along the lines of:
if (thereIsAHashInTheUrl) {
do this;
} else {
do this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
简单使用位置哈希:
Simple use of location hash:
输入以下内容:
Put the following:
如果 URI 不是文档的位置,则此代码段将执行您想要的操作。
If the URI is not the document's location this snippet will do what you want.
你试过这个吗?
(显然,其中
url
是您要检查的 URL。)Have you tried this?
(Where
url
is the URL you want to check, obviously.)这将解决问题;)
This will solve the problem ;)
将返回哈希标识符
will return the hash identifier
...或者有一个 jquery 选择器:
...or there's a jquery selector:
您可以使用 现代 JS 解析 url:
不带任何内容的 url哈希将返回空字符串。
You can parse urls using modern JS:
urls with no hash will return empty string.
您可以执行以下操作来定期检查哈希值的更改,然后调用函数来处理哈希值。
Here's what you can do to periodically check for a change of hash, and then call a function to process the hash value.
大多数人都知道 document.location 中的 URL 属性。 如果您只对当前页面感兴趣,那就太好了。 但问题是能够解析页面上的锚点而不是页面本身。
大多数人似乎忽略了这些相同的 URL 属性也可用于锚定元素:
Most people are aware of the URL properties in document.location. That's great if you're only interested in the current page. But the question was about being able to parse anchors on a page not the page itself.
What most people seem to miss is that those same URL properties are also available to anchor elements:
帕特里奇和加雷斯上面的评论很棒。 他们应该得到一个单独的答案。
显然,散列和搜索属性在任何 html Link 对象上都可用:
或者
如果您需要在常规字符串变量上使用它并且碰巧有 jQuery,
这应该有效:(
但它可能有点过头了..)
Partridge and Gareths comments above are great. They deserve a separate answer.
Apparently, hash and search properties are available on any html Link object:
Or
Should you need this on a regular string variable and happen to have jQuery around,
this should work:
(but its maybe a bit overdone .. )
将其作为从任意类似 URI 的字符串中抽象位置属性的方法放在这里。 尽管
window.location instanceof Location
为 true,但任何调用Location
的尝试都会告诉您它是非法构造函数。 您仍然可以通过将字符串设置为href
属性来获取诸如hash
、query
、protocol
等内容一个 DOM 锚元素,它将与window.location
共享所有地址属性。最简单的方法是:
为了方便起见,我编写了一个小库,利用它用一个接受字符串并生成
window.location
的构造函数来替换本机Location
构造函数 -类似对象: Location.jsThrowing this in here as a method for abstracting location properties from arbitrary URI-like strings. Although
window.location instanceof Location
is true, any attempt to invokeLocation
will tell you that it's an illegal constructor. You can still get to things likehash
,query
,protocol
etc by setting your string as thehref
property of a DOM anchor element, which will then share all the address properties withwindow.location
.Simplest way of doing this is:
For convenience, I wrote a little library that utilises this to replace the native
Location
constructor with one that will take strings and producewindow.location
-like objects: Location.js通常点击先于位置更改,
所以点击后设置TimeOut是个好主意
获取更新的 window.location.hash
或者您可以使用以下方式监听位置:
我编写了一个 jQuery 插件 来执行某些操作喜欢
你想做什么。
这是一个简单的锚路由器。
Usually clicks go first than location changes,
so after a click is a good idea to setTimeOut
to get updated window.location.hash
or you can listen location with:
I wrote a jQuery plugin that does something like
what you want to do.
It's a simple anchor router.
下面是一个返回
true
或false
(有/没有主题标签)的简单函数:在本例中返回
true
。基于@jon-skeet 的评论。
Here is a simple function that returns
true
orfalse
(has / doesn't have a hashtag):Returns
true
in this case.Based on @jon-skeet's comment.
这是测试当前页面 URL 的简单方法:
This is a simple way to test this for the current page URL:
我注意到所有这些答案主要检查 window.location.hash 并使得编写测试变得困难。
您还可以像这样从 url 中删除哈希:
最后您可以将逻辑组合在一起:
I noticed that all of these answers mostly check window.location.hash and make it difficult to write tests.
You can also remove the hash from a url like so:
And finally you can combine the logic together:
有时您会得到完整的查询字符串,例如“#anchorlink?firstname=mark”,
这是我获取哈希值的脚本:
sometimes you get the full query string such as "#anchorlink?firstname=mark"
this is my script to get the hash value: