Run javascript when user closes a page?

Is there any way to accomplish something like this?

Basically, after our users are finished with a page I want to evaluate some fields and perform a few actions on the values of those fields. But the fields might change several times, and I only want to run the script once, so I can’t really use a model action… unless I’m missing something?

I can create a ‘close’ button that runs multiple actions… but this doesn’t account for users closing the window. Do I have to just train our users to never close their browser windows, and always use the ‘close’ button?

Thanks!

You can use the window onbeforeunload event.

$(window).on('beforeunload',function(){<br>&nbsp; &nbsp;// If returnMessage is never defined, no warning message will be thrown<br>&nbsp; &nbsp;var returnMessage;<br>&nbsp; &nbsp;$.each(skuid.model.getModel('Contacts').getRows(),function(i,row){<br>&nbsp; &nbsp; &nbsp;&nbsp;if (!row.FirstName) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;returnMessage = 'One or more Contacts does not have a FirstName. Are you sure you want to leave the page without giving them a First Name?';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; &nbsp;&nbsp;}<br>&nbsp; &nbsp;});<br>&nbsp; &nbsp;return returnMessage;<br>});



More more details, see the docs:

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

Sweet! Thanks, Zach!