<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<button type="button" id="startTest">开始考试</button>
<button type="button" id="endTheTest">结束考试</button>
<!-- 防作弊代码相关 -->
<script>
function launchFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
function exitFullscreen () {
if (document.exitFullscreen) {
document.exitFullscreen();
}else if(document.mozCancelFullScreen){
document.mozCancelFullScreen();
}else if(document.webkitCancelFullScreen){
document.webkitCancelFullScreen();
}else if(document.msExitFullscreen){
document.msExitFullscreen();
}else if(document.oRequestFullscreen){
document.oCancelFullScreen();
}
}
function checkFull() {
var fullScreen = parent.document.webkitIsFullScreen || parent.document.fullscreen || parent.document.mozFullScreen || parent.document.msFullscreenElement;
if (fullScreen === undefined) {fullScreen = false;}
return fullScreen;
}
$(function(){
$('#startTest').click(function(){
launchFullscreen(document.documentElement);
})
$('#endTheTest').click(function(){
exitFullscreen(document.documentElement);
})
var fullscreenchangeList = ['fullscreenchange','mozfullscreenchange','webkitfullscreenchange','MSFullscreenChange']
fullscreenchangeList.forEach(function(ele){
document.addEventListener(ele, function () {
if (!checkFull()) {
alert("退出全屏")
}
}, false);
})
})
</script>