Jquery捕获警报对话?

时间:2011-03-04 11:08:58

标签: jquery

有没有办法从jquery中的普通javascript中捕获“alert()”框,这样当用户点击“OK”时我可以触发一个函数?

2 个答案:

答案 0 :(得分:0)

我相信无论用户是否取消或点击警报上的“确定”,它都会继续播放该脚本。您可以编写自己的警报功能,其中包含alert()和您自己的代码:

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

答案 1 :(得分:0)

我想出了一个更好的方法来做到这一点,并且通过替换alert函数中调用的函数来完成,我确信有更好的方法来存储前一个函数,但是这里是;

<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。如果你想在默认情况下关闭警报,那么在window.onload函数中调用changeAlertStatus()。这个答案不是'使用jquery,但我想你可以用document.ready而不是window.onload添加它。

(这也意味着您可以使用自己的界面创建自定义警报)

相关问题