为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用JavaScript检查URL中的#散列?

如何使用JavaScript检查URL中的#散列?

幕布斯7119047 2019-06-18 13:46:27
如何使用JavaScript检查URL中的#散列?我有一些jQueryJavaScript代码,只有在URL中有散列(#)锚链接时才能运行。如何使用JavaScript检查这个字符?我需要一个简单的所有捕获测试来检测这样的URL:example.com/page.html#锚example.com/page.html#另一个锚基本上类似于:if (thereIsAHashInTheUrl) {             do this;} else {     do this;}如果有人能指出正确的方向,那将是非常感谢的。
查看完整描述

3 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

简单:

if(window.location.hash) {
  // Fragment exists} else {
  // Fragment doesn't exist}


查看完整回答
反对 回复 2019-06-18
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

if(window.location.hash) {
      var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
      alert (hash);
      // hash found
  } else {
      // No hash found
  }


查看完整回答
反对 回复 2019-06-18
  • 3 回答
  • 0 关注
  • 302 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信