Solved

Intercept (and examine) windows alert message in Javascript

Posted on 2015-01-24
8
276 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 42

Expert Comment

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

Expert Comment

by:Rob Jurd, EE MVE
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
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40569708
Good to hear. Thanks for the points
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now