Created
March 3, 2016 18:51
-
-
Save cmacrander/c1eb2f1ed97f808a66a4 to your computer and use it in GitHub Desktop.
Qualtrics completion code javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var unloadEvent; | |
var listener; | |
Qualtrics.SurveyEngine.addOnload(function () { | |
'use strict'; | |
// initially hides completion code | |
$j('#completion-code').hide(); | |
var code = '${e://Field/mturk_code_part_1}'; | |
var suffix = '3781'; | |
var showCode = function () { | |
$j('#completion-code').html(codeDisplayStr).show(); | |
$j('#show-button').hide(); | |
}; | |
var codeParts = [code.substring(0, 3), code.substring(3), suffix]; | |
var codeDisplayStr = codeParts.join('-'); | |
// Shows button that user will need to click to get completion code. | |
$j('#show-button').click(showCode); | |
// Temporarily prevents user from going to next screen. | |
perts.temporarilyBlockNavigation( | |
"Write down your completion code before moving on. If you do not copy " + | |
"your completion code, you may not be able to get credit for this " + | |
"assignment.", | |
20 | |
); | |
// Create a pop-up message if user closes their window too soon. | |
var leavingMessage = "Are you sure you wrote down your completion code? " + | |
"You cannot return to this page."; | |
// Figure out the right unload event to use for this device. | |
if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) { | |
// iOS doesn't have a onbeforeunload event, but you can detect | |
// navigation away from the page. Use an alert(), which will block JS | |
// and give us a chance to show them a message first. They won't have | |
// an option to cancel their navigation (unlike onbeforeunload), so add | |
// the code to the alert window directly. | |
unloadEvent = 'pagehide'; | |
listener = function (evt) { | |
alert(leavingMessage + "\n\nYour code is: " + codeDisplayStr); | |
}; | |
} else { | |
unloadEvent = 'beforeunload'; | |
listener = function (event) { | |
event.returnValue = leavingMessage; | |
return leavingMessage; | |
}; | |
} | |
// Attach the popup to the unload event. | |
window.addEventListener(unloadEvent, listener, false); | |
}); | |
Qualtrics.SurveyEngine.addOnUnload(function () { | |
'use strict'; | |
// If the user clicks the next button after waiting for the 20 seconds, | |
// like they're supposed to, remove the popup. | |
window.removeEventListener(unloadEvent, listener, false); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! Do you know how I can load this into a qualtrics survey?