2011-03-04 95 views
0

有沒有辦法從jquery中正常的javascript捕獲「alert()」框,以便當用戶點擊「OK」時,我可以觸發一個函數?jquery捕獲警報對話?

+0

警報阻止,所以你應該能夠在線後做到這一點? – 2011-03-04 11:10:50

+0

我已經在下面回答你的問題,因爲我覺得這是你想要的。如果答案不正確,你的問題應該更清楚。 – AlanFoster 2011-03-04 11:41:43

回答

0

我想出了一個更好的辦法來做到這一點,這是通過更換稱爲報警功能中的函數來完成,我確定有一個更好的方法來存儲以前的功能,但在這裏;

<script> 
var stopAlerts = false, previousAlertCode; 
window.onload = function() { previousAlertCode = window.alert; } 
function changeAlertStatus(){ 
    stopAlerts = ! stopAlerts; 
    window.alert = (stopAlerts ? function(){ return false; } : previousAlertCode); 
} 
</script> 
<input type="button" value="testAlert" onclick='alert("test");' /> 
<input type="button" value="switch alerts" onclick='changeAlertStatus()' /> 

它也適用於ie8這是你想要的,我猜。 (基本上這意味着除非stopAlerts被設置爲false,否則任何對alert()的調用都將返回false如果你想讓alert在默認情況下關閉,那麼在window.onload函數中調用changeAlertStatus()這個答案是isn 「T使用jQuery的,但我想你可以用的document.ready,而不是在window.onload)添加它

(這也意味着,您可以創建自定義提醒過我想,用自己的接口)

0

我相信無論用戶是否取消或點擊提醒中的「確定」,它都會繼續執行腳本。你可以寫它結合了alert()和你自己的代碼你自己的報警功能:

function myAlert(message){ 
    alert(message); 
    //your code here 
}