I have written some code to block the F5 key on web page. It's working fine except when I have to display a large amount of data. If you press the F5 key during the loading duration in which the HTML and JavaScript code is generated, my page gets refreshed.
Here is the code that I am using to block the F5 key:
document.onkeydown = fn;
var fn = function (e){
if (!e)
var e = window.event;
var keycode = e.keyCode;
if (e.which)
keycode = e.which;
var src = e.srcElement;
if (e.target)
src = e.target;
// 116 = F5
if (116 == keycode) {
// Firefox and other non IE browsers
if (e.preventDefault) {
e.preventDefault();
e.stopPropagation();
// Internet Explorer
}else if (e.keyCode){
e.keyCode = 0;
e.returnValue = false;
e.cancelBubble = true;
}
return false;
}
});
I think this code is not working when the HTML and JavaScript code is generating.
I have some very simple code for preventing F5 that I use myself. Works in IE, Chrome and Firefox:
function disableButtonsDown(e) {
if ((e.which || e.keyCode) == 116) e.preventDefault();
};
$(document).on("keydown", disableButtonsDown);
document.onkeydown=disableF5;
var version = navigator.appVersion;
function disableF5(e)
{ var keycode = (window.event) ? event.keyCode : e.keyCode;
if ((version.indexOf('MSIE') != -1))
{ if (keycode == 116)
{ event.keyCode = 0;
event.returnValue = false;
return false;
}
}
else
{ if (keycode == 116)
return false;
}
}
Related
Print screen option has been disabled in Google Chrome with the below js code.
document.onkeydown = keydown;
document.onkeyup = keyup;
function keydown(e) {
console.log("key down triggered");
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (e.keyCode == 44 || e.keyCode == "44" || e.which == 44 || e.which == "44") {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.ctrlKey && (e.key == "P" || e.key == "C" || e.key == "A" || e.key == "p"||e.key == "c" || e.key == "a" || e.charCode == 16 || e.charCode == 112 ||e.keyCode == 80) || (e.keyCode == 44) || (e.keyCode == 123)) {
//alert("Inspect element & Print &cut/copy option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode > 111 && e.keyCode < 124) {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.key == "F11" || e.key == "f11") {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
}
function keyup(e) {
debugger;
console.log("key up triggered");
if (e.keyCode == 44 || e.keyCode == "44" || e.which == 44 || e.which =="44") {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode > 111 && e.keyCode < 124) {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.key == "F11" || e.key == "f11") {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
The above code works fine in Google Chrome.key code 44 for Print screen option is used.In IE all the function keys have been restricted,but the keyup and keydown function is not getting triggered,when in IE browser alone.
What is the alternate way to handle print screen option in IE Browser using jquery.?
And additionally the above code fails to prevent print screen when the user opens a bootbox alert pop up inside a web application.
Suggest a solution for the above 2 scenarios.
Don't.
No matter how clever your code, you can't stop me from focussing a different window on my second monitor and hitting Print Screen there to get a copy of whatever's on-screen that you're trying to stop me from screenshotting.
Or I could just open the browser console (even if you block F12 / Ctrl+Shift+I, I can still get there via the browser's menus) and type document.onkeydown = document.onkeyup = null; and break your entire guard.
It's not often that I "answer" a question by completely shutting it down, but you are wasting your time.
I am trying to do the trigger keyDown event on click of a button, but this is not working.
$("#button").click(function() {
var e = jQuery.Event("keydown");
e.keyCode = 37;
$(this).trigger(e);
return false;
});
But the event is not triggering. Can anyone suggest please?
It looks to me like it's working.
Try the snippet below with the test function:
$(document).ready(function() {
$("#button").click(function() {
var e = jQuery.Event("keydown");
e.keyCode = 37;
$(this).trigger(e);
console.log(e);
return false;
});
});
// test trigger
$(window).keydown(function(e) {
key = e.keyCode ? e.keyCode : e.which;
if (key === 37) {
alert(`Left arrow triggered, (keyCode ${key})`);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Trigger key 37 (left arrow)</button>
document.getElementById('search_field').onkeypress = function(e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
window.location.href = '/search/?s=' + $('#search_field').val();
return false;
}
};
The last bracket show me an error, not all code paths return a value, what seems to be problem here ?
Thanks
Try this :
document.getElementById('search_field').onkeypress = function(e) {
if (!e) {
e = window.event;
}
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
window.location.href = '/search/?s=' + $('#search_field').val();
return false;
}
return true;
};
More... I think that you may not use both pure javascript and jquery
So you'd rather choose between
JAVASCRIPT :
document.getElementById('search_field').onkeypress = function(e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
window.location.href = '/search/?s=' + document.getElementById('search_field').value;
return false;
}
return true;
};
JQUERY
$( "#search_field" ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
window.location.href = '/search/?s=' + $(this).val();
return false;
}
return true;
});
End your function with return true.
If any other key then 13 is pressed the flow should just continue normally.
Ignore your tool. Event handlers do not have to return a value in every occasion, it is fine if only a particular path does return false.
My code works without problems on IE9/IE10, FF, Chrome and opera but on older Internet Explorer no Keyboard input is handled.
I have the following code for handling events. It should only fire when a new button is pressed.
lastEvent = void 0;
heldKeys = {};
window.onkeydown = function(event) {
if (lastEvent && lastEvent.keyCode === event.keyCode) {
return;
}
lastEvent = event;
heldKeys[event.keyCode] = true;
switch (event.which) {
case 80:
return myamp.userInput("positiv");
case 81:
return myamp.userInput("negativ");
}
};
window.onkeyup = function(event) {
lastEvent = null;
return delete heldKeys[event.keyCode];
};
You need to bind to the document, not window.
window.onkeyup = function(event) {
window.onkeydown = function(event) {
needs to be
document.onkeyup = function(event) {
document.onkeydown = function(event) {
Try
lastEvent = void 0;
heldKeys = {};
window.onkeydown = function(event) {
event = event || window.event; //IE does not pass the event object
if (lastEvent && lastEvent.keyCode === event.keyCode) {
return;
}
lastEvent = event;
heldKeys[event.keyCode] = true;
var keyCode = event.which || event.keyCode; //key property also different
switch (keyCode) {
case 80:
return myamp.userInput("positiv");
case 81:
return myamp.userInput("negativ");
}
};
window.onkeyup = function(event) {
event = event || window.event;
lastEvent = null;
return delete heldKeys[event.keyCode];
};
You have to use normalized key code. Like this:
var keyCode = event.which || event.keyCode;
I tried different solutions but these still did not work correctly in IE8. What I came up with that worked is this:
window.onkeyup = function(e) {
e = (e) ? e : window.event; // check if e is defined
var kc = (e) ? e.which : e.keyCode; // assign keyCode
key = (key === undefined) ? e.keyCode : kc; // if keyCode still undefined, reassign it
if (kc == 13) {
//enter was pressed
}
// other code
}
Is there any way to trap ctrl+n key in chrome (by using javascript, jquery or any plugin)? I need to assign ctrl+n+enter key to particular task, but as soon as I press ctrl+n, chrome opens a new window. I am able to trap ctrl+n in firefox by using:
event.preventDefault()
but its not working in chrome.
This may work for your issue.
// defining flags
var isCtrl = false;
var isShift = false;
$(document).ready(function () {
// action on key up
$(document).keyup(function (e) {
if (e.which == 17) {
isCtrl = false;
}
if (e.which == 16) {
isShift = false;
}
});
$(document).keydown(function (e) {
if (e.which == 17) {
isCtrl = true;
}
if (e.which == 16) {
isShift = true;
}
if ((e.which == 110 || e.which == 78) && isCtrl) {
e.preventDefault(true);
}
});