Javascript event is not triggering - javascript

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>

Related

How can I prevent jQuery smartWizard from moving to the next step on enter press?

I have tried this code but it is not working.
var wizardForm = $('#form');
wizardForm.on('keyup keypress', function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
return false;
}
});
This article solved my queestion:
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});

keydown event listeners not working in javascript

in the code below i'm trying to get it to log "success" into the console when I press the "w" button, but for some reason it's not doing anything when I press it. can someone tell me what I'm doing wrong?
var keysDown = {};
var keysUp = {};
window.addEventListener('keydown', function(e) {
keysDown[e.keyCode] = true;
});
window.addEventListener('keyup', function(e) {
delete keysDown[e.keyCode];
keysUp[e.keyCode] = true;
});
if (37 in keysDown || 65 in keysDown) { //left
console.log("success");
}
window.addEventListener('keyup', function(e) {
console.log(e.keyCode)
if(e.keyCode == 37 || e.keyCode == 65) console.log('yay')
});
http://jsfiddle.net/zackify/anq34vsv/ Just check for the keycode in the event listener function.

How to disable Ctrl+U using Javascript

I just want to disable Ctrl+U and Ctrl+C event. The primary purpose of doing this is, preventing users from downloading any image or copying content from my website easily i.e. pressing Ctrl+U for viewing my webpage's source code or pressing Ctrl+C for copying content directly from my webpage.
Currently, I am using this piece of code but it disables my entire keyboard
<script>
/*function check(e)
{
alert(e.keyCode);
}*/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) {//Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
}
return false;
};
</script>
Your problem is the return statement.
Though I would suggest you use addEventListener and the like, this is a working copy of your code:
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
alert('not allowed');
return false;
} else {
return true;
}
};
This is finally what I got to disable Ctrl+U:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
return false;
} else {
return true;
}
};
$(document).keypress("u",function(e) {
if(e.ctrlKey)
{
return false;
}
else
{
return true;
}
});
</script>
Its simple just use the following code it will disable only Ctrl+U while Ctrl+C, Ctrl+V, Ctrl+S etc will works fine:
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 85 )) {
return false;
}
};
</script>
try check this link from jsfiddle.
js
shortcut.add("Ctrl+U",function(){
alert('Sorry\nNo CTRL+U is allowed. Be creative!')
}),
it will simple show and error when you try to hit Ctrl+U on your keybord
But check the link, there is alot of code
To disable right click
document.addEventListener('contextmenu', event => event.preventDefault());
To disable F12 options
document.onkeypress = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
return false;
}
}
document.onmousedown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
return false;
}
}
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
return false;
}
}
To Disable ctrl+c, ctrl+u
jQuery(document).ready(function($){
$(document).keydown(function(event) {
var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
if (event.ctrlKey && (pressedKey == "c" || pressedKey == "u")) {
alert('Sorry, This Functionality Has Been Disabled!');
//disable key press porcessing
return false;
}
});
});
This will Disable Ctrl + U and Right-click (make sure it's at the top of all your code) :
<!-- Disable CTRL U and Right Click -->
<script>
document.onkeydown = function(e) {
if (e.ctrlKey && e.keyCode === 85) {
return false;
}
};
</script>
<body oncontextmenu="return false;"></body>
<!-- disable CTRL U and Right Click -->
you can use the below script
<script>
/*function check(e)
{
alert(e.keyCode);
}*/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117 || e.keycode === 17 || e.keycode === 85)) {//ctrl+u Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
}
return false;
};
</script>
Its simple just use the following code it will disable only Ctrl+U while Ctrl+C, Ctrl+V, Ctrl+S etc will works fine: but it will disable your page source too.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript"> $(function () { $(this).bind("contextmenu", function (e) { e.preventDefault(); }); }); </script>
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 85 )) {
return false;
}
};
</script>

Not all code paths return a value (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=' + $('#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.

Block F5 key using JavaScript

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;
}
}

Categories

Resources