If I have a page overriding window.alert box:
Window.alert = function() {}
can I re-enable the alert box if I have access to the Javascript?
I guess I would save the original alert function off somewhere else and then reassign it back to it.
var originalAlert = window.alert;
window.alert = function(stuff) {
console.log('alert invoked');
}
alert(); // displays a message in the console
var newWindow = window.open();
window.alert = newWindow.alert;
newWindow.close();
alert(); // alerts a message
I don't know that I would open a new window just to get the alert function, but that function is native code, so once it's been closed over, you can't get it back without some crazy hack like this. At least not that I'm aware of.
Related
This question already has answers here:
JavaScript: Overriding alert()
(12 answers)
Closed 2 years ago.
I have many JavaScript functions for a program, all of which contain alerts notifying the user of certain things. I wanted to give the user the option to ban all of those alerts, without having to rewrite all of the functions without the alerts. Is there a way to ban alert? Maybe something like this:
<button onclick = "alertBan()">Alert Ban</button>
function alertBan(){
alerts = false;
}
Just assign an empty function to alert.
function alertBan(){
alert = function(){};
}
If you might need to reenable alerts later on, you can store window.alert in a variable when the page loads.
const oldAlert = alert;
function alertBan(){
alert = function(){};
}
function enableAlerts(){
alert = oldAlert;
}
Store the original alert for perhaps later use. Replace the alert through empty function or with setted parameter to write on console.
If need the normal alert back take the stored one back.
alertOrig = alert;
function alertBan(consoleLog){
alert = function(e) {
if (consoleLog) console.log(e);
}
}
function alertEnable(){
if ( alertOrig )
alert=alertOrig;
}
alertBan();
alert('No console + no alert');
alertBan(true);
alert('With console');
alertEnable();
alert('Normal alert');
I read a bunch of links and answers and nothing worked. So in controller I have this:
Session["MySession"] = "HelloMaybe";
and in the page I have this:
window.onbeforeunload = function () {
console.log("Before Unload Window");
var somethingElse = '#Session["MySession"]';
console.log(somethingElse);
};
I see that script is running because it prints my first log message but there is nothing in there for value of session.
How can I do this?
var somethingElse='#System.Web.HttpContext.Current.Session["MySession"].ToString()'
I need to add an alert before the page closes and all online searches say to directly assign to window.onbeforeunload, as in window.onbeforeunload = function(), and return a string, but this is not working for me no matter what I do.
function unload(evt) {
return "UNLOAD";
};
console.log("SET1")
window.addEventListener('beforeunload', unload)
window.onbeforeunload = unload;
console.log("SET2");
This issue is ocurring in chrome specifically
I'm trying to write a rule that conditions on the content of a javascript alert box. That is, I'm trying to fire a conversion script if the alert says "submission successful" vs "some error message". Can I access the string shown in the alert box so I can use it i.e. if alert message == "submission successful" fire the tracking script?
You could override alert so that you could track what is being alerted. For example:
window.alert = (function() {
var existingAlert = window.alert;
return function(message) {
console.log(message); // do your tracking here
existingAlert(message);
};
})();
You would need to be able to run this code before other code called window.alert.
I'm writing some Jasmine tests for some legacy javascript that produces an alert or a confirm at some points in the code.
At the moment where the alert pops up it pauses execution in the browser requiring me to press ok before going on.
I'm sure I'm missing something but is there a way of faking an alert?
Even better is it possible to find out what the message was for the alert?
Thanks for your help.
spyOn(window, 'alert');
. . .
expect(window.alert).toHaveBeenCalledWith('a message');
var oldalert = alert;
alert = jasmine.createSpy();
// do something
expect(alert).toHaveBeenCalledWith('message')
alert = oldalert
Another way is to do this in spec helper.
window.alert = function(){return;};
Or if you need the message.
var actualMessage = '';
window.alert = function(message){actualMessage = message; return;}
You simply create a spy, surprisingly with createSpy(), to mock the function that contains the alert. So you can do something like this:
beforeEach(function() {
functionWithAlert = jasmine.createSpy("functionWithAlert");
functionWithAlert("called as usual");
});
You can also do this to return something
oldFunctionWithAlert = jasmine.createSpy("oldFunctionWithAlert() spy").andCallFake(function() {
console.log("Doing some testing");
return "Test";
});
On a side note, I would suggest you replace the alert if possible with less disruptive alternatives. There are a ton of options out there like JQuery UI dialog.