开发者

Disable alert(); [duplicate]

This question already has answers here: Closed 12 years ago.

Code that is generated on my page that I cannot control contains an alert. Is there a jQuery or other way to disable alert() from working?

The javascript that is being generated that I want to disable/modify is:

function fndropdownurl(val)
1317 { var target_url
1318 target_url = document.getElementById(val).value;
1319 if (target_url == 0)
1320 {
1321 alert("Please Select from DropDown")
1322 }
1323 else
1324 {
1325 window.open(target_url);
1326 return;
1327 }
1328 } 

I want开发者_如何学Go to disable the alert on line 1321

Thanks


Simply overwrite alert with your own, empty, function.

window.alert = function() {};

// or simply
alert = function() {};


This works in Chrome and other browsers with the console.log function.

window.alert = function ( text ) { console.log( 'tried to alert: ' + text ); return true; };
alert( new Date() );
// tried to alert: Wed Dec 08 2010 14:58:28 GMT+0100 (W. Europe Standard Time)


You can try and make a new function function alert() { } , this is not going to do anything since is empty and will overwrite the existing one.


Try this:

alert("test");
alert = function(){};
alert("test");

The second line assigns a new blank function to alert, while not breaking the fact that it is a function. See: http://jsfiddle.net/9MHzL/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜