开发者

Javascript: Need to detect when a user revisits the page with a changed hash tag

开发者 https://www.devze.com 2023-03-05 19:18 出处:网络
Problem is (at least with my current browser), changing the hash tag and entering the new url doesn\'t trigger a refresh of the page, and thus I\'m not aware offhand how to detect when this hash tag c

Problem is (at least with my current browser), changing the hash tag and entering the new url doesn't trigger a refresh of the page, and thus I'm not aware offhand how to detect when this hash tag change has 开发者_StackOverflowbeen made.

Is there an elegant way to do this?


Check for a new location.hash


This is a nasty-ish solution, but you can poll the URL for a change every so often:

var hash = window.location.hash;

function poll() {
  if (window.location.hash != hash) {
    hash = window.location.hash;

    // Hash has changed. Do stuff here.
  }
}

setInterval(poll, 100);
0

精彩评论

暂无评论...
验证码 换一张
取 消