window.onbeforeunload function is not working for me in the salesforce lightning environment. I have a visualforce page styled using Lightning design system, we I view the page in classic the window.onbeforeunload works fine and pops out the standard not saved messagebox when I switch the tab. However, when I switch back to lightning and try the same(change the tab) then its not working.

window.onbeforeunload = function(e) {
  return 'changes not saves';

Anyone ever come across this scenario.? Please suggest on what I'm doing wrong here,

  • please post a minimal code sample of a component (.cmp+controller.js), so we can better help you out. if you have any errors obtained while debugging, please include those as well
    – glls
    Aug 1, 2017 at 3:14
  • Related: salesforce.stackexchange.com/questions/140083
    – sfdcfox
    Aug 1, 2017 at 4:09

1 Answer 1


From what I can tell, you can't use the beforeunload event because the framework does whatever it does in a way that breaks this event. You're not doing anything "wrong," it simply does not work. This may be fixed in a future release, or not. For now, it's probably better to either do nothing, or at least save the data in the sessionStorage object and allow the user to reload the data if they come back during the same session.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .