I've looked through the docs, so forgive me if this is written down somewhere. Is it possible to do something like window.onunload for a Lightning component? window.onunload (or .onbeforeunload) both work for the user refreshing the page or going to a different site, but I can't get an event to fire if they navigate to a different component within a Lightning app (I want to track time spent using certain components).

  • 1
    FYI, I stumbled across this post when trying to capture when a user attempts to leave a lightning component by clicking a different tab in Lightning Experience - the senior program architect from salesforce here confirmed with sfdc product management that this is currently not possible, but may be in the near future Jun 6, 2017 at 14:30

1 Answer 1


Why don't you call a function in the button you're using to close the component?

If you're using a close button or a navigation button that ends the component you can call a function on that button.

  • I considered that, but I'd also like to capture when a user leaves the page or closes the browser window (to be able to track time spent on component as well as prompt a user if they have unsaved edits). Oct 28, 2016 at 13:32
  • Well, to track de time you can just create a time script. Oct 28, 2016 at 17:31
  • the time* And for the unsaved edits you could call the function on the button Oct 28, 2016 at 17:31

You must log in to answer this question.

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