Handle URL fragment identifier (anchor) change event in Javascript
Solution 1
Google Custom Search Engines use a timer to check the hash against a previous value, whilst the child iframe on a seperate domain updates the parent's location hash to contain the size of the iframe document's body. When the timer catches the change, the parent can resize the iframe to match that of the body so that scrollbars aren't displayed.
Something like the following achieves the same:
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
storedHash = window.location.hash;
hashChanged(storedHash);
}
}, 100); // Google uses 100ms intervals I think, might be lower
Google Chrome 5, Safari 5, Opera 10.60, Firefox 3.6 and Internet Explorer 8 all support the hashchange
event:
if ("onhashchange" in window) // does the browser support the hashchange event?
window.onhashchange = function () {
hashChanged(window.location.hash);
}
and putting it together:
if ("onhashchange" in window) { // event supported?
window.onhashchange = function () {
hashChanged(window.location.hash);
}
}
else { // event not supported:
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
storedHash = window.location.hash;
hashChanged(storedHash);
}
}, 100);
}
jQuery also has a plugin that will check for the hashchange event and provide its own if necessary - http://benalman.com/projects/jquery-hashchange-plugin/.
EDIT: Updated browser support (again).
Solution 2
An event listener can be added for the hashchange
event, which will fire whenever the fragment identifier changes:
window.addEventListener('hashchange', function() {
// Perform action
// [...]
})
I would recommend this approach instead of overwriting window.onhashchange
, otherwise you will block the event for other plugins.
Looking at the global browser usage today, a fallback is longer needed, as modern browsers support this event.
Solution 3
setInterval()
is only universal solution for now. But there are some light in the future in form of hashchange event
Solution 4
From what I see in other SO questions, the only workable cross-browser solution is a timer. Check out this question for example.
Solution 5
(Just for the record.) The YUI3 "hashchange" synthetic event does more or less the same thing as the accepted answer
YUI().use('history-hash', function (Y) {
Y.on('hashchange', function (e) {
// Handle hashchange events on the current window.
}, Y.config.win);
});
Comments
-
Bogdan Gusiev over 2 years
How can I write the Javascript callback code that will be executed on any changes in the URL fragment identifier (anchor)?
For example from
http://example.com#a
tohttp://example.com#b
-
Frank Nocke about 11 yearsfor completeness, add
var storedHash = window.location.hash;
to the putting-it-together summary block. Btw: Nowadays this is called polyfill I think. -
Timo Huovinen over 10 yearsNowadays you can listen to the
hashChange
onwindow
stackoverflow.com/questions/6390341/how-to-detect-url-change -
Timo Huovinen over 10 years@AndyE I did read the answer, I missed the small note, and I have never seen events that look like
hashChanged(storedHash);
-
Andy E over 10 years@TimoHuovinen:
hashChanged
, in this case, is intended to be a function implemented by a developer using this code. The answer here just demonstrates how one may monitor the hash for changes where theonhashchange
event is not available, and combines the event-based and timer-based approaches to provide a universal solution. My point is that the answer that you linked to adds absolutely nothing to the answer here ;-). They provide the same basic information, even the link to Ben Alman's jQuery plugin. The only difference is that I provided a pure JS solution in my answer. -
Timo Huovinen over 10 years@AndyE ok, wasn't aware of the requirement to implement that function, thank you for clarifying
-
gskema almost 10 yearsI was looking for some lightweight JS router, but this did the job nicely. Also eliminated all dependencies :)
-
Admin almost 9 yearsFYI:
onhashchange
event's documentation at Mozilla Developer Network -
Raj over 6 yearsThis is about DOM changes.