Firstly, you need to capture the "beforeunload" event (which previous answers here did).
// Display browser warning message when back/forward/reload or hyperlink navigation occurs $(window).on('beforeunload', function(e) { console.log('page navigation captured'); return 'By leaving this page you will lose the data you have entered here.'; });
More importantly, you want to allow non-disrupted navigation from certain elements (e.g. links with class "allow-navigation"). The way to achieve is to remove the event-handler when your links of choice are clicked.
// Disable capture for certain elements $('.allow-navigation').on('click', function(e) { $(window).off('beforeunload'); console.log('page navigation allowed'); });
Will work for your links of the type:
<a href="#" class="allow-navigation">Click me</a>
This way you can control the page navigation. Browser back/forward/etc still open the confirmation box. But the links you choose will work without any disruption.
Hope this helps.