0

When using hash fragments for url manipulation with javascript, you can simply detect all changes in the url fragment part using onhashchange event. Is there any similar way to detect changes in the path part of url using HTML5 history API?

I know, I can override handling clicks on anchor elements, override back button behaviour, but what if the user edits url by hand?

Samuel Hapak
  • 6,423
  • 2
  • 30
  • 54
  • Please see: http://stackoverflow.com/questions/680785/on-window-location-hash-change – YMMD Apr 23 '13 at 07:41
  • The `onbeforeunload` might be what you need: https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload – Christofer Eliasson Apr 23 '13 at 07:41
  • But, is it possible to cancel the unload in the `onbeforeunload` handler? – Samuel Hapak Apr 23 '13 at 07:52
  • @SamuelHapak You can not forbid user from navigating out. – Passerby Apr 23 '13 at 08:15
  • @Passerby but that means, that `onbeforeunload` is totally unusable for Ajax navigation (that is navigating to another page without reloading) in single page applications. – Samuel Hapak Apr 23 '13 at 11:33
  • @SamuelHapak You can't "navigate to another page without reloading". Once navigated, the life cycle of current page is finished, and every script context is discarded. You can use AJAX to **modify page content** without navigating (i.e. not touching the URL of the page, except for the hash part), but you can't, by any means, forbids user from navigating out. – Passerby Apr 23 '13 at 11:41
  • @Passerby I do understand that I can't forbid the user from navigating out. But when he changes only the hash portion of the URL, I can notice it and change the page without navigation away. As HTML5 adds possibility to change the path portion of URL without navigating away, I was curious, whether it is possible to suppress the page reload in cases, where user do not change the domain part of the url, but only the path part. – Samuel Hapak Apr 23 '13 at 11:51

0 Answers0