You must fire the event manually when the page loads. The event is activated only with direct user action. Since this is a page load without any user action, the hashchange event does not fire.
$(window).on('hashchange', function (e) { alert(location.hash); }).trigger('hashchange');
If you want to fire an event only when there is a hash value, then
$(window).on('hashchange', function (e) { alert(location.hash); }); if (window.location.hash) { $(window).trigger('hashchange') }
Arun P Johny
source share