odd behavoir of alert in FF

Discussion in 'Javascript' started by Michael, Nov 8, 2006.

  1. Michael

    Michael Guest

    can anyone explain why I get an endless loop of alerts (alert - hit enter -
    alert - hit enter etc) from the following script/page?
    It works ok in IE, and works ok in FF if I use the mouse to click the "ok",
    but not if I use the enter key....

    <html>
    <head>
    <title>Test</title>

    <script type="text/javascript">
    function updateForm(dom)
    {
    var qty = document.getElementById(dom).value;

    if (qty != "")
    if (qty == 0 || qty > 5)
    {
    alert ("The number may not be 0 and not bigger than 5!");
    document.getElementById(dom).select();
    document.getElementById(dom).focus();
    }
    }
    </script>
    </head>

    <body>
    <form action="#">
    Enter a number:
    <!-- I changed value="0" to value="" -->
    <td> <input type = "text" name = "qty1" id = "qty1" value="" size="15"
    onkeyup = "updateForm('qty1');" /> </td>

    </form>
    </body>
    </html>

    regards

    Michael
    Michael, Nov 8, 2006
    #1
    1. Advertising

  2. Michael

    Randy Webb Guest

    Michael said the following on 11/7/2006 7:30 PM:
    > can anyone explain why I get an endless loop of alerts (alert - hit enter -
    > alert - hit enter etc) from the following script/page?
    > It works ok in IE, and works ok in FF if I use the mouse to click the "ok",
    > but not if I use the enter key....


    Because the Enter key triggers the onkeyup for the input (which is
    incorrect behavior to me).

    Why are you passing a string that is the ID of the input and then
    looking up that input with gEBI rather than just passing a reference to
    the field and it's value?

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Nov 8, 2006
    #2
    1. Advertising

  3. Michael

    Michael Guest

    "Randy Webb" <> wrote in message
    news:...
    > Michael said the following on 11/7/2006 7:30 PM:
    >> can anyone explain why I get an endless loop of alerts (alert - hit
    >> enter - alert - hit enter etc) from the following script/page?
    >> It works ok in IE, and works ok in FF if I use the mouse to click the
    >> "ok", but not if I use the enter key....

    >
    > Because the Enter key triggers the onkeyup for the input (which is
    > incorrect behavior to me).
    >
    > Why are you passing a string that is the ID of the input and then looking
    > up that input with gEBI rather than just passing a reference to the field
    > and it's value?
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ - http://jibbering.com/faq
    > Javascript Best Practices -
    > http://www.JavascriptToolbox.com/bestpractices/


    Thanks for the reply Randy.

    This was actually written by a fellow student, rather than me. I couldn't
    figure out why it was behaving as it was and our tutor wasn't helping much
    so I thought I'd ask here. I think this is probably a much cut own version
    of a larger script, hence it seems a bit round about in its method.

    So how does the keyup on the alert object cause the firing of the event
    registered to the <input> object? Why are they not separate?

    Thanks for your help

    Regards

    Michael
    Michael, Nov 8, 2006
    #3
  4. Michael

    buhailiang Guest

    it seems that when alert object receive onkeydown, it closed, and so
    onkeyup is send to the input object.

    "Michael дµÀ£º

    > So how does the keyup on the alert object cause the firing of the event
    > registered to the <input> object? Why are they not separate?
    >
    buhailiang, Nov 8, 2006
    #4
  5. Michael

    Lee Guest

    Michael said:
    >
    >can anyone explain why I get an endless loop of alerts (alert - hit enter -
    >alert - hit enter etc) from the following script/page?
    >It works ok in IE, and works ok in FF if I use the mouse to click the "ok",
    >but not if I use the enter key....
    >
    ><html>
    ><head>
    ><title>Test</title>
    >
    ><script type="text/javascript">
    >function updateForm(dom)
    >{
    > var qty = document.getElementById(dom).value;
    >
    > if (qty != "")
    > if (qty == 0 || qty > 5)
    > {
    > alert ("The number may not be 0 and not bigger than 5!");
    > document.getElementById(dom).select();
    > document.getElementById(dom).focus();
    > }
    >}
    ></script>
    ></head>
    >
    ><body>
    ><form action="#">
    >Enter a number:
    ><!-- I changed value="0" to value="" -->
    ><td> <input type = "text" name = "qty1" id = "qty1" value="" size="15"
    >onkeyup = "updateForm('qty1');" /> </td>
    >
    ></form>
    ></body>
    ></html>



    Performing an action on a key-up event is risky in any GUI
    language because you can never be sure that the corresponding
    key-down occurred in the same focus. That seems to be the
    problem here. The keydown event is seen by the alert window
    and the keyup is seen by the input field.

    Instead of "onkeyup", use "onkeypress" or "onchange".


    --
    Lee, Nov 8, 2006
    #5
    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. Markus Franz

    Processes with strange behavoir

    Markus Franz, Apr 4, 2004, in forum: Python
    Replies:
    2
    Views:
    339
    Peter Otten
    Apr 4, 2004
  2. Joachim Schmitz

    sscanf behavoir

    Joachim Schmitz, Apr 19, 2007, in forum: C Programming
    Replies:
    7
    Views:
    441
    Gunvant Patil
    Apr 20, 2007
  3. Replies:
    5
    Views:
    141
    YANAGAWA Kazuhisa
    Mar 8, 2006
  4. Sy Ys

    BackgrounDRb behavoir

    Sy Ys, May 27, 2007, in forum: Ruby
    Replies:
    1
    Views:
    109
    Ezra Zygmuntowicz
    May 27, 2007
  5. surf

    perl polymorphic behavoir ?

    surf, Feb 7, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    104
    A. Sinan Unur
    Feb 7, 2006
Loading...

Share This Page