My test case is simple:

I have an input, with an onclick event. When the user clicks the
input, an ajax request is sent. Simple. Works fine.

Now, I want to be a lil smarter, and make it so the onclick of the
input fires when the user presses some predefined keyboard key.
Escape, for example (27). So I add the new code to catch the escape
key (on the body), make it so the onclick of the input fires when
escape is pressed. Still fairly standard. Still working, up to that
point.

My problem is this:

When the user presses escape, the ajax fires, but in firefox (and
firefox only, not on IE or safari), I get an exception, and it's the
error code that fires instead. Only when I press select. If I still
click the button normally, it works fine.

The tricky part:

Now, I've found an ugly way for it to work. If, after creating my ajax
request object, og my onclick function, I fire up an alert of some
kind, then the ajax request works fine. Obviously, this doesn't really
fix anything. It just gives a hint as to what might be happening
behind the scenes.

Also, I can confirm it's not the event firing code per se that is
wrong. If I call the same code (the apply part) using another
technique, then the ajax also works. So it's really related to the key
press in some way.

The question:

Any idea what might be happening? What I'm doing wrong? I've included
my code below.

<code>
<html>
    <head>
        <title></title>
        <script type="text/javascript" src="js/prototype.js"></script>
        <script type="text/javascript">
            function doit(){
                alert("doin it");
                var URL = "XML?listener=something";     //not really
important
                var ajaxRequest = new Ajax.Request(
                    URL,
                    {
                        onSuccess: doit2,
                        onFailure: doit3
                    }
                );
                //alert("s");
            }

            function doit2(){
                alert("doit2");
            }

            function doit3(){
                alert("doit3");
            }

            function bodyKeyPress(event){
                var k;

                //get the key code
                if(event.keyCode){
                    k = event.keyCode;
                }

                if(k && k == "27"){
                    var element = document.getElementById("go");
                    element["onclick"].apply(element);
                }
            }


            function simulate(){
                //alert('go');
                var element = document.getElementById("go");
                element["onclick"].apply(element);
            }
        </script>
    </head>
    <body onkeypress="bodyKeyPress(event)">
        <center>
            <table border='1' style='margin: 300 0 0 0'>
                <tr>
                    <td>
                        <input type="button" id='go' value="Do It"
name="Do It" onclick='doit()' />
                    </td>
                    <td>
                        <input type="button" id='go2' value="Do It 2"
name="Do It 2" onclick='simulate()' />
                    </td>
                </tr>
            </table>
        </center>
    </body>
</html>
</code>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to