Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect where JavaScript alert is coming from?

I am trying to debug a very complex IE Intranet application. I am getting an alert with a custom message stating that an exception has occurred. I would like to find out more information about this exception as the message is not very helpful.

There is a master page which contains a lot of iFrames (and .htc files if that makes a difference) so I don't think that I can try and hijack window.alert. My last resort will be to try my luck with a file search.

Using IE 8, is there anyway I can detect where this alert is coming from? The ideal solution would be to somehow create a "breakOnAlert" function which inserts a debbuger statement at the correct alert location.

To clarify: The master page contains many iframes and I believe that the error+alert is coming from one of these. Each iframe is an aspx page (sometimes with dynamic html/javascript from the user) and contains inline and external JavaScript. Before posting I did try overriding alert in my page (a child page inside an iframe) but it didn't work. I am assuming that It doesn't work as each iframe has their own window object so they each have their own version of alert. For this to work I would need to find all iframes and override it for each one, something which I think would be very complicated to do. In the IE developer tools I can see a huge amount of script files (inline and external), so it would be very difficult to manually look for the alerts in there.

like image 836
row1 Avatar asked Apr 09 '12 08:04

row1


Video Answer


2 Answers

Since it's a real chore to do it in all iframes, I'd probably use Fiddler and programatically replace alert( with something like:

(function(n){alert(n);debugger;})(

IE should support the debugger statement, so you'd have a call-stack

This page explains how to do a text-replace in Fiddler

Example Fiddler custom rule to add to OnBeforeResponse:

if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html"))
{
    oSession.utilDecodeResponse();
    var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
    oBody = oBody.replace(/alert\(/gi, "(function(n){alert(n);debugger;})(");
    oSession.utilSetResponseBody(oBody);
}
like image 175
Madd0g Avatar answered Oct 20 '22 00:10

Madd0g


Ovveride alert function and set a breakpoint inside, then you can watch Stack Trace :)

 function alert(message) {
     var x = 'whatever';
 }


 $(function () {
     alert('test');
 });
like image 25
Michal Franc Avatar answered Oct 20 '22 00:10

Michal Franc