在IE 11中无法通过JavaScript禁用Ctrl + O. [英] can not disable Ctrl+O by JavaScript in IE 11
问题描述
我正在尝试在IE中禁用Ctrl + o组合键,以下代码在除IE 11之外的所有IE版本中都能正常工作,除非我在下面的代码中看到警告:
I'm trying to disable Ctrl+o key combination in IE, the following code works fine in all IE versions except IE 11 unless I do an alert as you see in code below:
document.onkeydown = function(event) {
var x = event.keyCode;
console.log(event.keyCode);
console.log(event.ctrlKey);
if ((x == 79) && (event.ctrlKey)) {
if(navigator.userAgent.match(/rv:11.0/i)){
alert('Disabled');
}
event.cancelBubble = true;
event.returnValue = false;
event.keyCode = 0;
event.stopPropagation();
event.preventDefault();
return false;
}
};
我想知道是否有其他人遇到同样的问题他们已经解决了。 :-)
谢谢。
Alex
I was wondering if anyone else is experiencing the same issue and they have solved it. :-) Thanks. Alex
推荐答案
我得出的结论与Alex&最大。在我的特定用例中,强制兼容模式会破坏其他功能。
I came to the same conclusion as Alex & Max. In my specific use case, forcing compatibility mode would break other features.
我相信在大多数情况下,确认对话框是最好的解决方法,因为它对用户来说仍然有点自然 - 除了所涉及的额外步骤。
I believe that in most cases a confirm dialog is the best workaround, as it still feels somewhat natural to the user - save for the extra step involved.
http://jsfiddle.net/dperish/sp72c0wt/3 /
HTML:
<h1>Demonstration of IE11 event bubbling issue</h1>
<label>Enable Workaround<input type="checkbox" id="enableWorkaround"></label>
<p>Pressing CTRL-P or CTRL-O should NOT show the default open/print dialogs. The only workaround seems to be to interrupt the main thread either with alert(), confirm(), or by hitting a breakpoint in a debugger. </p>
<p>Unfortunately, a synchronous/blocking XHR call was not useful for this purpose. Nor was using the browser-specific showModalDialog.</p>
<div id="output"></div>
Javascript:
Javascript:
function onKeyDown(e) {
e = e || window.event;
if ((e.keyCode === 79 || e.keyCode === 80) && e.ctrlKey) {
e.preventDefault();
e.stopPropagation();
e.returnValue = false;
if ($("#enableWorkaround").is(":checked")) {
if (confirm("Run some custom method?")) {
customMethod(e.keyCode);
}
}
else {
customMethod(e.keyCode);
}
return false;
}
}
function customMethod(x) {
$("#output").append("<p>CustomMethod Says: KeyCode = " + x + "</p>");
return false;
}
$(document).ready(function() {
$(document).on("keydown", function (e) {
onKeyDown(e);
});
});
这篇关于在IE 11中无法通过JavaScript禁用Ctrl + O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!