You need to sign in to do that
Don't have an account?

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,
window.onbeforeunload = function(e) {
return 'changes not saves';
Anyone ever come across this scenario.? Please suggest on what I'm doing wrong here,
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
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.