location.reload doesn't reload the page when I'm using it in keyDown event handler when ESC is pressed. Only in FF

firefox, javascript, jquery

Solution

That's because the Esc key stops the refresh immediately after you fire it in Firefox.

Use a `setTimeout` so the `location.reload()` will execute after the event has already finished bubbling.

jQuery(document).keydown(function (event) {
    if (event.keyCode == 27) {
        setTimeout(closeVideoPopup, 0);
    }
});

Fiddle

Or better, just call `event.preventDefault()` so the Esc key won't cancel the page reload:

jQuery(document).keydown(function (event) {
    if (event.keyCode == 27) {
        event.preventDefault();
        closeVideoPopup();
    }
});

Fiddle

Problem

I find out that location.reload() call doesn't do anything when I called to it from keyDown event handler when ESC button is pressed. Does anybody knows some workaround how to reload the page? Also I find http://bugs.jqueryui.com/ticket/4922 and it looks like this problem is already fixed. Here is a sample of code. subscription to event: ``` jQuery(document).keydown(function (event) { if (event.keyCode == 27) { closeVideoPopup(); } }); ``` And closeVideoPopup() method: ``` function closeVideoPopup() { jQuery('#fade, .window_container').fadeOut(function(){ jQuery('#fade, a.close').remove(); }); jQuery('ul.tabs').css('z-index', '99'); jQuery('div.framing_slider').css('z-index', '9999'); location.reload(); return false; ``` } Please note this code works perfect in all browsers except FF.

Original source