0

I want to create a Webpage which is used to conduct online examination so if user switches to other tabs, the examination must be ended after 1st view and current tab must be closed.

I already used page visibility API in Javascript to find the number of views user switches to other tabs.window.close() is not working in chrome.

if(document.hidden==true){
      views++;
      if(views==1){
        close_window();return false();
      }

      alert( "Your view count is: <b>" + views +
      ". " + "Your page current state is: " +
      document[(prefix === "" ? "v" : prefix + "V") + "isibilityState"] + "</b><br />");

  }
  }
  function testPageVisibilityApi() {
    if (prefix === null)
      document.getElementById("log").innerHTML = "Your browser does not support Page Visibility API";
    else {
      document.addEventListener(prefix + "visibilitychange", countView);
      countView();
    }
  }

Any suggestions?

Ali
  • 2,702
  • 3
  • 32
  • 54

3 Answers3

0

To close the particular tab, you can implement function like:

(function() {
    'use strict';
    $("#close_page").click(function() {
        var confirm_result = confirm("Are you sure you want to quit?");
        if (confirm_result == true) {
            window.close();
        }
    });
})();
prateek3636
  • 155
  • 1
  • 7
0
var Activetab;

window.onfocus = function () { 
 Activetab = true; 
}; 

window.onblur = function () { 
 Activetab = false; 
}; 

// check periodically 
setInterval(function () { 
  console.log(window.Activetab ? 'active' : 'inactive'); 
 }, 2000);

this may help you to detect when the user changes tabs, this is working fine and referred from here UPDATED

var Activetab;

window.onfocus = function () { 
 Activetab = true; 
}; 

window.onblur = function () { 
 Activetab = false; 
 // window.close();
 open("yourpathoffile/filename.ext", '_self').close(); // or you can 
try directly window.close
}; 

// check periodically 
setInterval(function () { 
  console.log(window.Activetab ? 'active' : 'inactive'); 
 }, 2000);
CodeBug
  • 1,649
  • 1
  • 8
  • 23
  • but ,could you suggest me how to close that tab if tab is inactive? – Smily Smily Jun 18 '20 at 04:22
  • i have only js part their is no need of any HTML part, if I understood ur problem properly u need to close the tab of ur examination if user changes tab, the code I provided will do the same thing, when user switches between tabs it close your exam tab. – CodeBug Jun 18 '20 at 09:11
0

JS:

function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}

HTML:

<a href="javascript:close_window();">close</a>

or:

<a href="#" onclick="close_window();return false;">close</a>

We added return false here to prevent the default behavior for the event. Otherwise the browser will attempt to go to that URL.

Refer to this article for Firefox.

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
  • I already used same code in Microsoft edge browser ,but that browser is not allowing tab to close without user confirmation,If user clicks on cancel,exam will be continued,This is not the thing to be happened @Ankit Jindal – Smily Smily Jun 18 '20 at 08:42
  • @SmilySmily I think this should work. window.open('', '_self', ''); window.close(); – Ankit Jindal Jun 19 '20 at 08:40