function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforceapexsalesforceapex 

window.onbeforeunload not working when using in Salesforce lightning

Hi - 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,
NagendraNagendra (Salesforce Developers) 
Hi,

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.

Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
salesforceapexsalesforceapex
Thank you for your response. This is really surprising that framework doesn't allow this basic window.onbeforeunload event. My screen loads with lot of data but let me try. thank you
Mustafa JhabuawalaMustafa Jhabuawala
As per my knowledge, window.onbeforeunload will not work because in lightning everything loads in a seperate iframe.

In Lightning - So basically in Lightning when your visualforce is loaded the window is already prepared behind the scene

In Classic - Whevener you are going to your specific tab the page is loaded i.e (whole Classic is loaded) that's why you can handlet the window events.

Hope you understood.

To achieve your functionality you need to use jQuery plugin and you need to write some events like .ready or other depending on your requirement.

Let me know if you find this useful.
salesforceapexsalesforceapex
thank you. I will check I can write any jQuery to accompolish this. Do you have any sample jQuery doing this kind of functionality. Requirement is simple, when I switch to a different tab, have to through some message. Any help would be appreciated.
Mustafa JhabuawalaMustafa Jhabuawala
Are those tabs created in the same component or you are talking about browser tab ?
salesforceapexsalesforceapex
Sorry for the delayed response. Actually its VF page Tab. Where as we can edit the form and in classic it works fine. However, as I said before when we switch to lightning when I move from the VF page tab to different tab message not pops out for unsaved changes. I understand tabs are iframes in Lightning but not sure how to accompolish through jQuery either