javascript - window beforeunload is not working as expected in jquery -
basically trying was, whenever user tries close current tab(when on site), want display pop 3 choices why leaving , want store choice where
so have written following in main.js
loaded through entire site pages
$(document).ready(function() { // before closing current tab, ask user reason $(window).on('beforeunload', function(event){ $('#load_choices_up').click(); event.stoppropagation(); event.preventdefault(); debugger; }); });
so have 3 issues above jquery code
*.this code executing when click link on same page(i mean if navigate page current page), want code run when current tab/page closed(about close) completely, not when navigating page on site
*. after line $('#load_choices_up').click()
executed, choices pop opening expected, default processing of browser(that closing functionality) not being stopped 2 lines event.stoppropagation();
, event.preventdefault();
, mean these 2 methods of stopping behaviour not working , browser closed, want processing based on user choices input , based on close tab.
*. when used return "why want leave page"
, instead of choices pop up, browser displaying different message based on browser type "you have unsaved changes" in chrome, , different message in firefox, instead of displaying custom message
so finally, why event.stoppropagation();
, event.preventdefault();
not working ? , why can't able display custom message ?
you can't prevent closing browser. obvious security reasons. imagine spam-website preventing closing website while pumping full of god knows what.
you can @ pull 1 function alert()
or prompt
. after user closes them, tab close either way.
beforeunload
extremely short-timed. won't able run massive scripts it, user close tap before script run properly. (i tried ajax call, didn't work)
so, if you're able options want in there, moment user chooses 1 of options, you're not going able save anywhere. script never make far.
you can customize "are sure?" message so:
$(document).ready(function() { window.onbeforeunload = function(e) { return 'dialog text here.'; }; });
but again, can change text. it's browser's native functionality, , cannot change it.
Comments
Post a Comment