Hi, I have added the custom code found on a community post to a question in Qualtrics. The code displays a pop-up message asking participants not to leave the survey screen. The code works well when a participant clicks into the search bar or opens a new tab in the same window. However, if they leave the window (by clicking on the desktop for example), the code glitches quite heavily. Specifically, when they return to the survey screen, the pop-up message continuously pops on and off and the participant can’t interact with the survey. This happened for one test participant using Safari and another test participant using Chrome. However, a 3rd test participant using Chrome said it worked fine. Is there anyway to update the code below to avoid this bug? Thank you!
Qualtrics.SurveyEngine.addOnload(function() {
// Requires 'leaveCount' Embedded Data field initialized in Survey Flow
let count = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('leaveCount')) || 0;
window.addEventListener('blur', function() {
count++;
Qualtrics.SurveyEngine.setEmbeddedData('leaveCount', count);
// Optional: Trigger alert or set warning flag
alert("Please do not switch tabs during the reflection.")
});
});
