41

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

Thanks

specked
  • 519
  • 1
  • 6
  • 14

4 Answers4

111

Simply overwrite alert with your own, empty, function.

window.alert = function() {};

// or simply
alert = function() {};
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
21

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)
joar
  • 15,077
  • 1
  • 29
  • 54
  • 2
    Indeed, if you avoid the default behavior of alert() it is a good idea to at least output alert() calls to console.log(). Just overriding alerts puts you on a much more obscure scenario. Your code can fail and because you supressed al alerts (that may contain important info) you may be never acknolowedged. Cool. – m3nda Feb 18 '18 at 15:46
9

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.

Vlad.P
  • 1,464
  • 1
  • 17
  • 29
6

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/

James Wiseman
  • 29,946
  • 17
  • 95
  • 158