Skip to main content
Nintex Community Menu Bar
Question

Run javascript when user closes a page?

  • July 9, 2024
  • 2 replies
  • 76 views

Forum|alt.badge.img+18

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!

This topic has been closed for replies.

2 replies

Forum|alt.badge.img+13

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


Forum|alt.badge.img+18
  • Author
  • July 9, 2024

Sweet! Thanks, Zach!