I tried to add a keydown event with javascript that refreshes page. But it did not work. The same code works perfectly on button click event. How to achieve reloading page with keydown event? The page needs to be loaded with GET, even if the page first was loaded with POST, so window.location.reload() doesn’t work.
This issue is happening with FF 4. I haven’t tried other browsers yet.
The code that does NOT work is:
$(document).keydown(function(e) {
if (e.keyCode == 27) {
window.location.href = document.URL;
}
});
This code works perfectly:
$('.my-button-class').click(function(){
window.location.href = document.URL;
});
Try using “e.which” instead of “e.keyCode”. Browsers vary on that issue, but jQuery makes sure that “which” can be used reliably to get the key code.
edit — Firefox 4 does seem to act funny, but you can work around the behavior by reloading the page in a new event: