Detect whether javascript threw an alert window (with javascript)

Discussion in 'Javascript' started by umdsasha@gmail.com, Aug 24, 2006.

  1. Guest

    So, basically, I need to detect whether an alert window was thrown. I
    can't find where it's thrown from but I need to disable a button only
    if there were no alert windows thrown. Any ideas?

    Thanks
    Alex
    , Aug 24, 2006
    #1
    1. Advertising

  2. guywmustang Guest

    If you're using a try{ } catch {}
    Just add a variable in there to increment a counter... check to see if
    the counter was > 0 ... How are you getting this alert to come up?


    wrote:
    > So, basically, I need to detect whether an alert window was thrown. I
    > can't find where it's thrown from but I need to disable a button only
    > if there were no alert windows thrown. Any ideas?
    >
    > Thanks
    > Alex
    guywmustang, Aug 24, 2006
    #2
    1. Advertising

  3. RobG Guest

    wrote:
    > So, basically, I need to detect whether an alert window was thrown. I
    > can't find where it's thrown from but I need to disable a button only
    > if there were no alert windows thrown. Any ideas?


    Presumably, you are the one calling alerts so surely you can do this
    some other way? Anyhow, all you do is assign a reference to
    window.alert to some other global variable, then assign your own
    function to window.alert - you *must* do this in the right order or
    you'll lose your one and only reference to the window.alert function.

    Disable the button by default, then enable it with your replacement
    alert function. Of course users without JavaScript won't be able to
    enable the button, but then they can't call alerts either. :)

    e.g.

    <script type="text/javascript">

    // Function to run when alert called
    function trapAlert(msg){
    document.getElementById('aButton').disabled = false;
    xAlert(msg);
    }

    // Assign reference to window.alert to another variable
    var xAlert = window.alert;

    // Re-assign window.alert
    window.alert = trapAlert;

    </script>

    <input type="button" value="Button to enable" id="aButton" disabled
    onclick="alert('I\'m working!!');">
    <input type="button" value="Call an alert" onclick="alert('hey');">


    --
    Rob
    RobG, Aug 24, 2006
    #3
  4. Hi,

    > wrote:
    >> So, basically, I need to detect whether an alert window was thrown. I
    >> can't find where it's thrown from but I need to disable a button only
    >> if there were no alert windows thrown. Any ideas?
    >>
    >> Thanks
    >> Alex


    guywmustang wrote:
    > If you're using a try{ } catch {}
    > Just add a variable in there to increment a counter... check to see if
    > the counter was > 0 ... How are you getting this alert to come up?


    try catch will catch an exception, not an alert window. You were
    probably confused by the use of "thrown". alert windows are not thrown,
    they are displayed ;-)

    HTH,
    Laurent


    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Aug 24, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary

    What Page threw the exception

    Gary, Jan 23, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    366
    Steven Cheng[MSFT]
    Jan 28, 2004
  2. ruca

    "The server threw an exception"

    ruca, Jun 1, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    716
  3. Dave

    LOOP THREW A REPEATER CONTROL

    Dave, Jul 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,610
    Karl Seguin
    Jul 29, 2005
  4. Mersh
    Replies:
    0
    Views:
    488
    Mersh
    Mar 13, 2007
  5. Ganesh
    Replies:
    0
    Views:
    551
    Ganesh
    Jun 29, 2007
Loading...

Share This Page