I'm new to js. How can I check if the client press the key combination ctl+s?
$(document).keypress(function(e) {
if(e.charCode == 17 && e.charCode == 83) {
alert("ok");
}
});
This doesn't work. Jquery is included.
I'm new to js. How can I check if the client press the key combination ctl+s?
$(document).keypress(function(e) {
if(e.charCode == 17 && e.charCode == 83) {
alert("ok");
}
});
This doesn't work. Jquery is included.
This would work if you are jQuery.
$(document).ready(function() {
$(document).keydown(function(event) {
if (event.ctrlKey && event.keyCode == 83) {
event.preventDefault()
//action here
alert('Saved');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Press ctrl +s
Working fiddle here.
If you want to detect cmd + s
on mac, you need to use keycodes mentioned below on different-different browsers, beacuse unlike Shift/Alt/Ctrl, Mac/Apple key is not considered as a modifier key--instead, you should hook on keydown/keyup and record when a key is pressed:
Firefox: 224
Opera: 17
WebKit (Safari/Chrome): 91 (Left Apple) or 93 (Right Apple)
I got from here.
And if you want close your browser tab on save window.top.close();
will work that I got from here. Also working for me.