asual / jquery-address

jQuery Address - Deep linking for the masses
http://www.asual.com/jquery/address
GNU General Public License v2.0
815 stars 173 forks source link

_preventDefault doesn't work with HTML5 history api #162

Open rhec opened 11 years ago

rhec commented 11 years ago

It works with the hash change method, but when using the HTML5 history api it will generate an error because window.history.popState is not a method that exists. You can see it is not listed in the mozilla documentation and I've confirmed in the console that it does not exist in Chrome. I'm not sure what browser, if any, implements it. I haven't been able to figure out a workaround, since the state has already changed by the time my event handlers are called, and there is no way to remove items from the history once you've pushed them on there. Perhaps, instead of using preventDefault to cancel the event after it has occurred, it would make sense to add a beforeChange function that can stop the change from occurring at all by returning false? Similar to how the form onsubmit event is handled.

asual commented 11 years ago

Thanks for reporting that!

This feature was contributed some months ago and I haven't looked much into it until now. I revised it yesterday and basically dropped most of the code. Please, pull the latest version and check if it works for you.

Event cancellation should work fine for internal events but I'm not sure I'll be able to enable it for external events like the browser back button. I did some tests but I wasn't able to prevent the default popstate event and I still haven't checked how the hashchange event behaves in the same situation.