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

jquery - Hiding and Showing default javascript alert - Stack Overflow

programmeradmin5浏览0评论

How to hide and show the default Javascript alert in the same HTML Page.

For example:

In function_one I want to hide the Javascript default alert

function function_one() {
    //hide Javascript alert
}

In function_two I want to show the default Javascript which has been hidden in function_one

function function_two() {
    //show Javascript alert
}

Both Javascript functions are in the same HTML page.

How to hide and show the default Javascript alert in the same HTML Page.

For example:

In function_one I want to hide the Javascript default alert

function function_one() {
    //hide Javascript alert
}

In function_two I want to show the default Javascript which has been hidden in function_one

function function_two() {
    //show Javascript alert
}

Both Javascript functions are in the same HTML page.

Share edited Aug 1, 2014 at 12:34 Marco Bonelli 69.8k21 gold badges127 silver badges146 bronze badges asked Aug 1, 2014 at 12:19 ThavamaniThavamani 2,1261 gold badge14 silver badges13 bronze badges 5
  • 1 What do you mean by "hide Javascript alert"? Which alert? – Ben Commented Aug 1, 2014 at 12:21
  • You mean the native alert() function? – putvande Commented Aug 1, 2014 at 12:22
  • Yes i want to hide and show window.alert() ; – Thavamani Commented Aug 1, 2014 at 12:23
  • You can't.. it can only be hidden by user interaction. – putvande Commented Aug 1, 2014 at 12:24
  • This question is really unclear. Why define 2 functions where you hide/show alert()? Just have, for example, a click() function showing an alert - otherwise it will not. Eg: $('#test').click(function () { alert('Alert!'); }); – urbz Commented Aug 1, 2014 at 12:24
Add a ment  | 

2 Answers 2

Reset to default 6

If you want to prevent calls to alert() from being displayed, use the following code:

var defaultAlert = null;

function function_one() {
  if (defaultAlert === null) {
    defaultAlert = window.alert;
    window.alert = function() {};
  }
}

function function_two() {
  if (defaultAlert !== null) {
    window.alert = defaultAlert;
    defaultAlert = null;
  }
}

You can disable it by overriding its value with an empty function. You store the old value in a variable (oldAlert) and then restore it assigning the old value back to window.alert:

function function_one() {
    oldAlert = window.alert;
    window.alert = function() {};
}

function function_two() {
    window.alert = oldAlert;
}
发布评论

评论列表(0)

  1. 暂无评论