Home >Web Front-end >JS Tutorial >Capture keyboard events (and compatible with various browsers)_javascript skills

Capture keyboard events (and compatible with various browsers)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:30:161230browse
Example: Block browser F5 refresh and refresh iframe instead
Copy code The code is as follows:

// Capture F5 event
$("body").keydown(function(e){
var ev = window.event || e;
var code = ev.keyCode || ev.which;
if (code==116) {
// Prevent the default F5 event
if(ev.preventDefault) {
ev.preventDefault( );
}else {
ev.keyCode=0;
ev.returnValue=false;
}
// Call the refresh function
Refresh();
}
});
//Refresh the current frame
function Refresh() {
document.getElementById('right-main-frame').contentWindow.location.reload();//Support IE
}

Other keyboard events:
code==13; //Enter event
code==37; //Left arrow key
code==38;//Up arrow key
code==39;//Right arrow key
code==40;//Down arrow key
code ==8;//Backspace delete key
code==116;//F5 refresh key
code==78;//Ctrl n
code==121;//Shift F10
code==122;//F12
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn