简体   繁体   中英

safari browser keydown event not working

I am trying to catch command+r (safari). Below code works for chrome,IE,Firefox

 $(document.body).on("keydown", this, function (event) { if (event.ctrlKey && event.keyCode == 82) { alert('ctrl+r event fired.') } if (event.metaKey && event.keyCode == 82) { alert('command+r event fired.') } }); 
Need help with catching this event on safari browser.

I want to show custom message to user on when user clicks command+r on safari browser.

As cmd-R forces a page reload on Safari you cannor catch this key combination.

Before the event is captured the JavaScript execution is halted and the page reloaded.

For this reason you cannnot even override the browser default behavior with evt.preventDefault(); as this code never gets executed.


As cmd-R is universally "reload the page" for Safari users then it would be bad user experience to override that behaviour with a different one (even if it would be possibile).

I think avoiding cmd-R to be overridden by JavaScript code is intentional by Safari developers. What if you override cmd-R (reload), cmd-Q (quit), cmd-W (close window). From a UX perspective you would "break" the appication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM