Jquery capture alert dialogue?
Is there a way to capture the "alert()" box from normal javascript in jquery, such that when the user 开发者_运维问答clicks "OK" I can fire a function?
I have come up with a better way to do it, and It's done by replacing the function called within the alert function, i'm sure there's a better way to store the previous function, but here it is;
<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()' />
It also works on ie8 which is what you wanted I guess.
(Basically it means that any call to alert() will return false unless stopAlerts is set to false. If you want alerts to be off by default, then call changeAlertStatus() within the window.onload function. This answer isn't using jquery, but I guess you could add it with document.ready rather than window.onload)
(it also means that you can create custom alerts too i guess, with your own interface)
I believe regardless of whether the user cancels or clicks 'OK' on an alert, it just continues the script. You could write your own alert function which incorporates the alert()
and your own code:
function myAlert(message){
alert(message);
//your code here
}
精彩评论