In a browser how can I make the keyboard's escape key go back in Javascript.
For example: if you visit this page and click the "Fullscreen" link I'd like to press the escape key and go back to the previous page.
What's the Javascript to make this magic happen?
You can add a Key-Listener:
window.addEventListener("keyup", function(e){ if(e.keyCode == 27) history.back(); }, false);
This will call history.back() if the Escape key (keycode 27) is pressed.
$(document).bind("keyup", null, function(event) {
if (event.keyCode == 27) { //handle escape key
//method to go back }
});
You can bind an onkeyup event handler to window and check if the keycode is 27 (keycode for Escape), then use the window.history.back() function.
window.onkeyup = function(e) {
if (e.keyCode == 27) window.history.back();
}
MDC docs on window.history, https://developer.mozilla.org/en/DOM/window.history
Just listen for key code 27 and call history.go(-1);
You need to listen for the 'ESC' keypress, and fire off the back action when it is pressed, like so:
document.onkeydown = function(e){
if (window.event.keyCode == 27) {
history.go(-1);
}
};
Related
Pressing esc in most browsers stops the request. However I want to override this and continue the request.
I have tried the following but no success.
$(document).on("keyup", function(e) {
if (e.keyCode == 27) { // escape key maps to keycode `27`
e.preventDefault();
}
});
Is this possible to achieve?
try this one
document.attachEvent("onkeydown", keydown);
function keydown() {
// esc
if (event.keyCode = 27) {
event.returnValue = false;
event.keyCode = 0;
}
}
F5 key code can be used but I don't want to depend on key code since it depends on OS.
$(document).on("keydown", function (e) {
if (e.which || e.keyCode === 116) {
isRefresh = true;
}
});
You can add a handler to window.onbeforeunload:
window.onbeforeunload = function(event){
console.log(event);
}
See: mdn documentation
I want to disable the "Enter" button on the keyboard so that when the user press enter to submit the form, nothing happens, and doing something else rather than submitting the form, such as alerting "Using keyboard is not allowed."
Here is what I have done, #calculator is a button:
$(document).ready(function(){
$("#calculator").keydown(function(){
console.log("Enter is disabled.");
return false;
});
});
Currently on its submission the form results unexpectedly (for instance redirects to the target page but without any CSS loaded.
$(document).on('keypress', function(e){
if(e.which == 13) {
e.preventDefault();
}
});
You can use .keypress() event to check which key was pressed then check the code of the key using e.keycode or e.which, if it's 13 then prevent submitting form:
$(document).keypress(function(e){
var code = e.keyCode || e.which;
if (code == 13 ) {
e.preventDefault();
return false;
}
});
For it, don't use submit button.
Use a div, style it like a button and submit the form using javascript on click of the div :)
Try this
For Disabling Keyboard
document.onkeydown = function (e) {
alert('Using keyboard is not allowed')
}
For Disabling Enter
$(document).on('keypress', function(e){
if(e.which == 13) {
e.preventDefault();
}
});
I know it is possible to use JQuery.Event to simulate "Enter" keypress. Something like this does that -
var event = jQuery.Event( "keydown" );
event.keyCode = 13
$("input").trigger( event );
However, this does not fire the browser default behavior when the actual "Enter" key is pressed. For example, going to next line if the cursor is in textarea or submitting the form when the cursor is in input tag.
I want to know if it is possible to trigger or simulate the actual "Enter" keypress event/behavior.
Try this : Js fiddle
var e = jQuery.Event("keypress");
e.which = 13
$("#test").keypress(function(){
alert('keypress triggered')
}).trigger(e)
Do you want some thing similar this one?
$(function(){
$(document).keypress(function(event) {
if(event.which == 13) {
console.log("Dude! You hit Enter Key!!");
}
});
});
you can use this:
$("body").delegate("input", "keypress",function(e){
if(e.which == 13){
}
}
it's work for all input .
Is there any way how to submit form when you press some predefined key on your keyboard (for example ; key)? I was thinking about something with onkeypress, but dont know how to set it just for one specific key.
Yes, and you were right with thinking onkeypress, just pass in the event, and check which key is pressed with event.which:
function keyPressed(event) {
if (event.which == 186) //keycode for semi-colon
console.log("Semi-colon pressed!");
}
}
Now just attach this function to a keypress handler.
Edit: Got the keycodes from here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
You'll want to get the keycode and submit the form if it's the right keycode.
To get the keycode from an event, do:
$(document).on("keypress", function(event) {
var keyCode = event.keyCode;
var keyWhich = event.which;
if(keyCode = 'yourkey' || keyWhich = 'yourkey') {
$(form).submit();
}
});
For a full list of keycodes to replace 'yourkey' with, I'd recommend something like this cheat sheet. Just type your key in the input and use whatever value it provides as your function's logic
You can do this in jQuery:
$(document).ready( function() {
$(document).keydown(function(e){
if (e.keyCode == 186) { // ; key
$('#theform').submit();
}
});
});
See fiddle.