简体   繁体   中英

How to detect where JavaScript alert is coming from?

I am trying to debug a very complex IE Intr.net 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.

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);
}

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

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


 $(function () {
     alert('test');
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM