Good afternoon,
I was testing my survey in Chrome, works well. In Edge, it is not showing the JavaScript part (not supported in Edge). Ideas or suggestions?
Java Code
How do I have two scripts to run on the same page? Right now I have it as:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
var display = $('wordCountDisplay');
var questionID = this.questionId;
var textbox =$('QR~' + questionID);
var that = this;
function countWords(s){
s = s.replace(/\\n/g,' '); // newlines to space
s = s.replace(/(^\\s*)|(\\s*$)/gi,''); // remove spaces from start + end
s = s.replace(/ ]{2,}/gi,' '); // 2 or more spaces to 1
if(s == ''){
return 0;
}else{
return s.split(' ').length;
}
}
textbox.onkeyup = function(e){
display.update(countWords(textbox.value));
console.log(textbox.value);
}
});
Qualtrics.SurveyEngine.addOnReady(function() {
setTimeout(myTimeout1, 180000)
setTimeout(myTimeout2, 300000)
function myTimeout1() {
document.getElementById("demo").innerHTML = "You have been writing for 3 minutes, please take the next 2 minutes to finish your writing prompt.";
}
function myTimeout2() {
document.getElementById("demo").innerHTML = "You have been writing for 5 minutes, please finish your writing prompt.";
}
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
Hi there, I think I found the threads you used to get this started. First, the method that uses HTML. The below is working for me, also in Edge. Make sure your Text Entry question's HTML is set up something like the below:
Click to write the question text
Your word count is: 0
Then, add the below to the question's JavaScript in the Onload section, changing the Milliseconds as needed:
var display = $('wordCountDisplay');
var questionID = this.questionId;
var textbox =$('QR~' + questionID);
var that = this;
function countWords(s){
s = s.replace(/\\n/g,' '); // newlines to space
s = s.replace(/(^\\s*)|(\\s*$)/gi,''); // remove spaces from start + end
s = s.replace(/ ]{2,}/gi,' '); // 2 or more spaces to 1
if(s == ''){
return 0;
}else{
return s.split(' ').length;
}
}
textbox.onkeyup = function(e){
display.update(countWords(textbox.value));
console.log(textbox.value);
}
setTimeout(function(){ document.getElementById("demo1").innerHTML = "You have been writing for 3 minutes, please finish your writing prompt." }, 3000);
setTimeout(function(){ document.getElementById("demo1").style.display = "none"; }, 5999);
setTimeout(function(){ document.getElementById("demo2").innerHTML = "You have been writing for 5 minutes, please finish your writing prompt." }, 6000);
Also in those threads, I found an alternative solution that uses just JavaScript. The JS that counts words is provided towards the bottom by TomG, and the JS that sends an alert to the respondent's browser is provided by AnthonyR. Together, you could add the below to the question's JS:
var q = jQuery("#"+this.questionId);
var input = q.find(".InputText");
input.after("
var display = q.find(".wordCount");
countWords(input.get(0));
input.on("input", function() { countWords(this) });
input.blur(function() { this.value = this.value.trim(); });
function countWords(el) {
if(el.value.length > 0) display.text(el.value.match(/\\S+/g).length);
else display.text("0");
}
setTimeout(function(){
alert("You have been writing for 3 minutes, please finish your writing prompt");
}, 3000);
setTimeout(function(){
alert("You have been writing for 5 minutes, please finish your writing prompt");
}, 6000);
Thank you so much!
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.