Forum Discussion
Is there anyway to execute javascript when a learning module is closed?
I'm creating a self-assessment tool and I'm using javascript to send variables to a Google sheet. I'd like a way to track some information on learners who are not completing the assessment tool. Specifically how far into the assessment they go before they close out of it.
I have one question per slide and have T/F variables that trigger when the learner hits the next button.
Is there anyway to have a javascript code trigger when the user simply closes the module? I feel like I could build a close button and assign the javascript to trigger when it's used, but suspect most won't use it instead of simply closing their tab.
- MathNotermans-9Community Member
There is. The window beforeunload event just does that.
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
There are some differences how and when it is called. Eg. you donot want it to trigger when a user changes to another tab in the browser, this event occurs whenever a user navigates away from a page and even on a reload. So principally this is called when the user closes his browser... but you have to figure out an approach to prevent calling it ( or adding conditions you check ) when its not needed... reload and changing to a tab i guess.