最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable alert Message - Stack Overflow

programmeradmin0浏览0评论

How can I disable certain alert on the page and allow others?

I tried With this code:

window.alert = function ( text ) {
   console.log(text); 
   if(!text.includes("rambo"))
      alert("rambo"); 
};

This won't work because it calls alert again and not the alert.

I need to use javascript alert( not any other libraries)

How can I disable certain alert on the page and allow others?

I tried With this code:

window.alert = function ( text ) {
   console.log(text); 
   if(!text.includes("rambo"))
      alert("rambo"); 
};

This won't work because it calls alert again and not the alert.

I need to use javascript alert( not any other libraries)

Share Improve this question edited Mar 27, 2018 at 12:25 Ilyas karim 4,8124 gold badges34 silver badges48 bronze badges asked Mar 21, 2018 at 5:14 Edwin ThomasEdwin Thomas 4865 silver badges16 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 15

Save a reference to the old window.alert first.

const oldAlert = window.alert;
window.alert = function ( text ) {
  console.log(text); 
  if(!text.includes("rambo"))
    oldAlert(text); 
  return true;
};

window.alert('ram');
window.alert('rambo');

The other two answers are mostly correct, but they pollute the global namespace by creating a new reference to window.alert. So I would suggest wrapping this in an IIFE:

(function() {
  var nativeAlert = window.alert;
  window.alert = function(message) {
    if (message.includes("test")) {
      nativeAlert(message);
    }
  };
}());

alert("Hello"); // Doesn't show up.
alert("Hello test"); // Works.

nativeAlert("test"); // Throws an error.

You could go a step further an create an alert function generator that creates an alert object using a predicate:

function alertGenerator(predicate) {
  if (typeof predicate === "function") {
    return function(message) {
      if (predicate(message) === true) {
        window.alert(message);
      }
    }    
  } else {
    return undefined;
  }
}

// Create an alert generator that requires the word "test" in it:
var testAlert = alertGenerator(t => t.includes("test"));
testAlert("Hello"); // Doesn't show up.
testAlert("Hello test"); // Works.

// Create an alert generator that requires the word "Hello" in it:
var helloAlert = alertGenerator(t => t.includes("Hello"));
helloAlert("Hello"); // Works.
helloAlert("Hello test"); // Works.
helloAlert("Test"); // Doesn't work.

You can store old alter in variable

var ar = alert;
window.alert = function(text) {
    console.log(text);
    if (!text.includes("rambo"))
        ar("rambo");
    return true;
};

alert('dfs');

If you don't wan't to pollute global namespace or use an IIFE, why don't you simply wrap window.alert in another function like this:

function myCustomAlert(message) {
  return message.includes('rambo') ? 
     window.alert(message)
   : false;
}

myCustomAlert("This message won't be shown!");

myCustomAlert("This message will be shown because it contains rambo");

发布评论

评论列表(0)

  1. 暂无评论