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, aclick()
function showing an alert - otherwise it will not. Eg:$('#test').click(function () { alert('Alert!'); });
– urbz Commented Aug 1, 2014 at 12:24
2 Answers
Reset to default 6If 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;
}