Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript (jquery), is there a way to detect when the page anchor changes?

Basically, I want to have links throughout the page that change the anchor -- the part of the URL after a # symbol. When that part of the URL changes, the JavaScript should respond.

I could add an onclick method to every link, but is there a better way?

like image 805
Ken Kinder Avatar asked Jan 27 '26 23:01

Ken Kinder


2 Answers

That's not an anchor, it's the hash?

$(window).on('hashchange', function() {
    alert('My fracking hash changed to : '+document.location.hash);
});
like image 96
adeneo Avatar answered Jan 30 '26 13:01

adeneo


$(window).bind('hashchange', function() {
  // code here
});

think that should do it

like image 33
joevallender Avatar answered Jan 30 '26 13:01

joevallender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!