Solved

Intercept (and examine) windows alert message in Javascript

Posted on 2015-01-24
8
320 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 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