Javascript 禁用警报();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4388248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:13:17  来源:igfitidea点击:

Disable alert();

javascriptjquery

提问by specked

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?

在我无法控制的页面上生成的代码包含警报。是否有 jQuery 或其他方法来禁用 alert() 工作?

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

我要禁用/修改的正在生成的 javascript 是:

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 to disable the alert on line 1321

我想禁用第 1321 行的警报

Thanks

谢谢

回答by Andrew Moore

Simply overwrite alertwith your own, empty, function.

只需alert用您自己的空函数覆盖即可。

window.alert = function() {};

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

回答by joar

This works in Chrome and other browsers with the console.logfunction.

这适用于 Chrome 和其他具有该console.log功能的浏览器。

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)

回答by Vlad.P

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.

您可以尝试创建一个新函数function alert() { },这不会做任何事情,因为它是空的,并且会覆盖现有的函数。

回答by James Wiseman

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/

第二行为 alert 分配了一个新的空白函数,同时不破坏它是一个函数的事实。见:http: //jsfiddle.net/9MHzL/