Solved

Intercept (and examine) windows alert message in Javascript

Posted on 2015-01-24
8
305 Views
Last Modified: 2015-01-25
I am trying to intercept the Windows Alert Message and decide whether to modify it (or to even to display it).
I have been working with the  code below, but it is displaying the  Alert first, then acting on "do something here".
I need to examine the Alert Message and either display as is, display a modified version or prevent it's display altogether.
Note: The solution only needs to work in Chrome
Thank you

(function() {
  var proxied = window.alert;
  window.alert = function() {
    // do something here
    return proxied.apply(this, arguments);
  };
})();
0
Comment
Question by:GE_Dave
  • 5
  • 3
8 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 40568897
window.alert = function(e) {
    e.preventDefault();
    // do something here
    return proxied.apply(this, arguments);
}
0
 
LVL 43

Expert Comment

by:Rob
ID: 40568898
You may also need:

e.stopPropagation();
0
 

Author Comment

by:GE_Dave
ID: 40569238
Thank You.
I will experiment with this later in the day...
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40569655
Sorry, I've given you the wrong info here (my brain must've been in another mode yesterday!)

I thought it was an event you're trying to capture but you're wanting to override the alert function.  Given that's the case, this fiddle shows you how to customise it for your own purposes.  If at any point you want to suppress the alert, you just use return;

Note: The code below utilises jQuery
window.old_alert = window.alert;

window.alert = function(message, fallback){
    if(fallback)
    {
        old_alert(message);
        return;
    }
    $(document.createElement('div'))
        .attr({title: 'Alert', 'class': 'alert'})
        .html(message)
        .dialog({
            buttons: {OK: function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 'auto'
        });
};

// New Alert
//alert('This is a <strong>new</strong> alert!');

// Old Alert:
//alert('This is an old, boring alert.', true);
// Or:
//old_alert('This is an old, boring alert.');

Open in new window

0
 

Author Comment

by:GE_Dave
ID: 40569680
Thank you.
I have worked with that code as well.  However, it assumes that I am generating the alert (requires fallback).
My goal is to stop an erroneous Alert message from being shown, by intercepting, checking the message and blocking it if the a specific message).  I am doing this because it will be several weeks before the code without the erroneous Alert will be pushed to production.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40569682
so you need to include the code I've posted before anything else on your page. Then it will work as any call to "alert(...)" will be intercepted by your function.
0
 

Author Comment

by:GE_Dave
ID: 40569705
Thank you again.  
I was over complicating it.

window.old_alert = window.alert;

window.alert = function(message){
  var myTestString = 'Bad Alert';
  if (message == myTestString){
    return;
  }
  old_alert(message);
};

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 40569708
Good to hear. Thanks for the points
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I am having a  Git   issue 6 42
Binding a Grid in Javascript 5 42
Why is initMap returning "not a function" error. 3 23
Only allow one check box 17 25
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question