Opera Preventdefault() on Keydown Event

Opera preventDefault() on keydown event

Opera doesn't support preventDefault on keydown, only on keypress.

As you can see in this example, you should bind a separate keypress handler for Opera (adapted to your situation):

var cancelKeypress = false;

document.onkeydown = function(evt) {
evt = evt || window.event;
cancelKeypress = (evt.ctrlKey && evt.keyCode == 84);
if (cancelKeypress) {
return false;
}
};

/* For Opera */
document.onkeypress = function(evt) {
if (cancelKeypress) {
return false;
}
};

disable Opera function key in javascript

To prevent the default action of a keypress in Opera, you need to do it in the keypress event. The following is adapted from my answer to this similar question:

var cancelKeypress = false;

document.onkeydown = function(evt) {
evt = evt || window.event;
cancelKeypress = /^(112|113)$/.test("" + evt.keyCode);
if (cancelKeypress) {
return false;
}
};

/* For Opera */
document.onkeypress = function(evt) {
if (cancelKeypress) {
return false;
}
};

Cancel the keydown in HTML

If you're only interested in the example keys you mentioned, the keydown event will do, except for older, pre-Blink versions of Opera (up to and including version 12, at least) where you'll need to cancel the keypress event. It's much easier to reliably identify non-printable keys in the keydown event than the keypress event, so the following uses a variable to set in the keydown handler to tell the keypress handler whether or not to suppress the default behaviour.

Example code using addEventListener and ignoring ancient version of Opera

document.addEventListener("keydown", function(evt) {
// These days, you might want to use evt.key instead of keyCode
if (/^(13|32|37|38|39|40)$/.test("" + evt.keyCode)) {
evt.preventDefault();
}
}, false);

Original example code from 2010

var cancelKeypress = false;

document.onkeydown = function(evt) {
evt = evt || window.event;
cancelKeypress = /^(13|32|37|38|39|40)$/.test("" + evt.keyCode);
if (cancelKeypress) {
return false;
}
};

/* For pre-Blink Opera */
document.onkeypress = function(evt) {
if (cancelKeypress) {
return false;
}
};

event.preventdefault is not working in firefox

As @epascarello and @Alon don't use alert for debugging instead use console logs, Removing alert from your script might help.

Hope i helped

Prevent select dropdown from opening in FireFox and Opera

$(function() {
$('select').on('focus', function(e) {
this.blur();
window.focus();
});
});

FIDDLE

Works in Firefox atleast, but does'nt seem to work in Chrome ?

EDIT

I could'nt come up with a decent way of detecting whether one method works or not, so did some browser sniffing instead. Not really the best way to do it, but the best I could come up with, and it does seem to work in the browsers I've tested :

$(function() {
$('select').on('focus mousedown', function(e) {
if ($.browser.webkit||$.browser.msie) {
e.preventDefault();
}else{
this.blur();
window.focus();
}
});
});​

preventDefault doesn't work on pageup or pagedown in Chrome or Safari

There are a couple issues with the code.

You can't prevent a default action of a key on keyup. By that time the code has already run. It has to be on keydown.

The second problem is that the keyCode is an integer not a string so your if statement is not going to be entered into.

http://jsfiddle.net/ymr744p4/

$(window).keydown(function(e) {
console.log(e.keyCode);
if(e.keyCode === 34) {
console.log('page down');
e.preventDefault();
}
});

event.preventDefault() works on Chrome, Firefox, but not Safari

Seems like this is problem with ctrlKey. Assuming you use Mac OS X system you need to check for metaKey too, so your code should be:

if ((event.ctrlKey || event.metaKey) && (event.keyCode == 61 || event.keyCode == 187))


Related Topics



Leave a reply



Submit