如果URL有散列标签和用户JavaScript函数刚刚导航向前或向后与浏览器? [英] JavaScript function if url has hash tag and user has just navigated forward or back with browser?

查看:106
本文介绍了如果URL有散列标签和用户JavaScript函数刚刚导航向前或向后与浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刷新页面时,如果散列标签在结束下会触发警报 twoB

The following will fire an alert when the page is refreshed if the hash tag ends in twoB:

if (window.location.href.match(/\#twoB/))
{
   alert('twoB');
}

问题是,如果你点击了几哈希标签,然后单击回到您的浏览器,虽然URL包括散列警报不火。我怎样才能让code类火灾(最好只)的用户已经回到或者与他们的浏览器转发之后。

The issue is that if you've clicked on a few hash tags and then click back in your browser, although the URL includes the hash the alert doesn't fire. How can I make the code fires (ideally only) after a user has gone back or forward with their browser.

我使用jQuery这个项目,所以我想在一个理想的世界一个jQuery的解决方案,如果语法是比较容易。

I'm using jQuery for this project so i would like a jQuery solution in an ideal world if the syntax is easier.

推荐答案

起初我解决了这个相当使用的setInterval反复检查URL的一种丑陋的方式。 HTML5s
当页面第一次加载运行onpopstate,然后每向前和向后导航以及。

Initially I solved this quite an ugly way of using setInterval to repeatedly check the URL. HTML5s onpopstate runs when the page is first loaded and then every forward and back navigation aswell.

window.onpopstate = function(){ 
  if (window.location.href.match(/\#twoB/))
  {
     alert('twoB');
  }
}

这篇关于如果URL有散列标签和用户JavaScript函数刚刚导航向前或向后与浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆