2016-05-12 116 views

回答

1

它不是很好的做法強制用戶在網頁上停留,如果他們不希望,但你可以有周圍的一些工作,如果你想他們離開之前的瀏覽器選項卡

function internalHandler(e) { 
    return "Please don't leave the page you can be fail in exams!"; 
} 
if (window.addEventListener) { 
    window.addEventListener('beforeunload', internalHandler, true); 
} else if (window.attachEvent) { 
    window.attachEvent('onbeforeunload', internalHandler); 
} 

如果你阻止用戶將其關閉,以確認關閉事件任何你無法控制ALT + F4或從任務管理器關閉的方式

您可以使用JavaScript這樣

var message = "You have not completed exam. Are you sure you want to leave?"; 
window.onbeforeunload = function(event) { 
var e = e || window.event; 
if (e) { 
    e.returnValue = message; 
} 
return message; 
}; 

它做,你可以卸載它,當用戶完成考試

window.onbeforeunload = null; 

,或者你可以用C#Windows窗體創建自己的瀏覽器應用程序。您可以在此設置此自定義選項而無需關閉按鈕。您可以輕鬆地在Windows窗體應用程序中加載Web應用程序表單

1

onbeforeunload & onunload將幫助你。您無法禁用,但可以向用戶顯示警報。

var showMsgTimer; 
window.onbeforeunload = function(evt) { 
    var message = 'Don't Discard'; 
    showMsgTimer = window.setTimeout(showMessage, 500); 
    evt = evt || window.evt; 
    evt.returnValue = message; 
    return message; 
} 

window.onunload = function() { 
    clearTimeout(showMsgTimer); 
} 

function showMessage() { 
    alert("You're Right!"); 
} 

如果這不是您所期望的。然後請嘗試https://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/