1

I'm creating an online quiz application.

  • The quiz has a timer which will start counting down when the user start the quiz.
  • The user is suppose to submit the answers when he finish the quiz.
  • If he runs out of time, the answers are automatically submitted.

Now what I need is

  • When a user start a quiz, I need to restrict him from opening other tabs or windows(To avoid use of google or any other sources where he can search for the answer). i.e., restrict him from minimizing the quiz window or opening any new tab/window/application

OR

  • If he happen to move away from the quiz window(meaning he opens a new window or something), the answers should get submitted automatically and the result page should be displayed next time he come back to the quiz page.

I'm assuming this question does not require the code I use since the question is a general one. If the code is in fact needed, I'm sorry, please tell me and I will post here. I'm sorry if this is a duplicate question. I tried searching but I don't really know what keywords I should be looking for.

Any help is appreciated.

Anagh
  • 373
  • 1
  • 5
  • 15
  • Just image the first approach were possible and think about the implications it would have if any arbitrary website could restrict a user in such severe ways. – Yoshi Sep 06 '13 at 10:15

3 Answers3

2

As mentionned by Vlad Preda, make sure to prevent form submission when javascript is not activated.

That said, as far as I know, there is no way to prevent the user from trying to cheat with some javascript in a webpage. One solution could be to warn him about which actions are forbidden (like resizing the window), then listen to available browser events to cancel the quizz if he breaks one of these rules. However, it seems hard to predict every scenarios and make all this compatible with every browsers.

A simpler solution could be to display one question after the other, giving the focus to the related form field, then to close the question as soon as this field loses focus. You could also disable the form field when it looses focus during some amount of time. Following code should do the job :

var intervalId;
var countDown = 10; // seconds
jQuery(formField).focus(function () {
    intervalId && clearInterval(intervalId);
});
jQuery(formField).blur(function () {
    intervalId = setInterval(function () {
        if (!(countDown--)) {
            alert('Too long to answer!');
        }
    }, 1000);
});
1

There is no 100% sure way of doing this, since Javascript can be de-activated.

However, there is a solution to check if a window has focus or not: JavaScript / jQuery: Test if window has focus

When the page loses focus, you can open a confirm box to leave the quiz, or whatever logic you need.

Community
  • 1
  • 1
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
0

You can use jQuery http://jquery.com/

$(window).blur(function(){
  //your code here
});
$(window).focus(function(){
  //your code
});