I have tried a couple of different ways to do the following (using
unobtrusive javascript) as well as straight javascript. So far the
only way that I have been able to get it to work is through straight
javascript but still is not working the way that I would hope.
Basically I have the following in my application.js
var Myapp = {
myObserver: function(event) {
// links to adding a new issue to the comic
if(event.keyCode == 97) alert(''a Pressed'');
}
}
in my view I have:
<script type="text/javascript" charset="utf-8">
Event.observe(document, ''keypress'', Myapp. myObserver);
</script>
This works perfectly fine in Safari (don''t have a windows box to test
on right now so not sure about IE) but it does not work at all in
Firefox. The alert box is supposed to appear when the letter a is
pressed on the page.
(I have prototype loaded). The wierd thing is if i change the
event.keyCode == 97 to event.keyCode == Event.KEY_TAB firefox works.
I tried using UJS at one point and had the events working in Safari
again by assigning body:keypress the above action but firefox hated
that completely.
I also need to be able to remove the observer when I make a call to
RedBox plugin which opens up a form. I can do this in safari no
problem when using the following code:
<%= link_to_remote_redbox ''Click Here'', {:url =>
my_url_path
(@object), :method => :get, :loading => "Event.stopObserving
(document,''keypress'', Myapp. myObserver);"} %>
And I reset the observer if I click on the close link without
submitting the form without a problem at all. But again firefox hates
this.
Lastly I can''t figure out how to send a variable to my custom
function. I want to send the user to a different URL when they hit
certain keyboard shortcuts when they are on the page but no matter
what I try the stopObserving will not work.
Any suggestions? am I going down the wrong path? anyone have a good
tutorial? I have tried everything I could find on google and have not
had much luck.
Thanks again for the response