Solved

Intercept (and examine) windows alert message in Javascript

Posted on 2015-01-24
8
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Geolocation works...sometimes 2 40
How to extreme from web browser to the internet 2 49
Change background images after 5 seconds. 12 48
Problem to setting 16 48
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.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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.

751 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