JavaScript Keycode issues - works almost with ctrlKey

I've a written a JavaScript that opens a specific page in the "content" div when I click on a link.

HTML:

<li><a target='_blank' href='#' onclick="load_shift_test()">Soft Drinks</a></li>

JS:

function load_shift_test(){
    document.getElementById("content").innerHTML='<object type="text/php" data="mineralwater.php" ></object>';

This works, however, I need it to choose between 2 pages if a certain key is pressed when clicking on the link so I rewrote the script this way:

function load_shift_test(){
    document.getElementById("content").innerHTML='<object type="text/php" data="mineralwater.php" ></object>';
 if (event.ctrlKey) 
    document.getElementById("content").innerHTML='<object type="text/php" data="mineralwateredit.php"></object>';

}

This also works but it will also opens a new tab in Chrome and I really don`t like it.

I would ideally like the page to open if the key "e" (without ctrl) is pressed. How can I do this?

Answer:1

The problem here is Chrome is overriding Ctrl+click as a new window shortcut before triggering any JS event. If you want "e" to be a modifier key, create a jQuery event that tracks the state of the key position.

window.keyDown = false; //Start with key down is false
$(document).keyup(function(event) { //On key up switch to false
  window.keyDown = (event.keyCode == 69) ? false : window.keyDown; 
} ).keydown(function(event) { //On key down switch to true
  window.keyDown = (event.keyCode == 69) ? true : window.keyDown; 
} );

Then you can take action in load_shift_test() by checking the global variable window.keyDown.

Answer:2

I'm using Select2 Plugin in my asp.net mvc 5 application. according to the documentation The placeholder option allows you to pass in a data object instead of just a string if you need more ...

I'm using Select2 Plugin in my asp.net mvc 5 application. according to the documentation The placeholder option allows you to pass in a data object instead of just a string if you need more ...

  1. select2 placeholder not displaying
  2. select2 placeholder not showing

I'm trying to shift an image around based on the device orientation. But the values are very jumpy at rest, going between -1 to +2 with no movement, and I need a way to smooth it out a bit. Is there ...

I'm trying to shift an image around based on the device orientation. But the values are very jumpy at rest, going between -1 to +2 with no movement, and I need a way to smooth it out a bit. Is there ...

I've used jQuery version 2.2.4 and tried to capture event - no luck. Is there any way to fix issue? This code works: window.addEventListener('test.namespace', e => console.log('CustomEvent with ...

I've used jQuery version 2.2.4 and tried to capture event - no luck. Is there any way to fix issue? This code works: window.addEventListener('test.namespace', e => console.log('CustomEvent with ...

  1. dispatch custom event with data

I have a 3 block of image. I want a css in which hover a block A transform block C and B, hovering a block B transform block A and C or hovering a block C transform block A and B You can see a demo ...

I have a 3 block of image. I want a css in which hover a block A transform block C and B, hovering a block B transform block A and C or hovering a block C transform block A and B You can see a demo ...