0

How can i get a url opens in a new tab by pressing a key on my keyboard?

I was trying with window.open but i got stuck when i try to use the keycode of the key that i want for example if i pressed y youtube opens or if a pressed g google opens in a new tab. That's what want to do but im lost on how to, i hope someone could help me. I was trying this window.addEventListener('keydown', function(e) {var link = document.querySelector('a[data- key="${e.keyCode}"]') --- to select the key if (!link) return; window.open }); this is how i think it could be but really i don't know

22nd Oct 2017, 8:51 PM
Ilean
Ilean - avatar
6 Respostas
+ 1
then you just need to check by : document.addEventListener('keydown', function(event){ { if(event.keycode == your key code) { window.open("youtube.com") } }
22nd Oct 2017, 9:36 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
0
I'd really apreciate any help. Thanks in advance
22nd Oct 2017, 8:52 PM
Ilean
Ilean - avatar
0
window.open("example.com")
22nd Oct 2017, 9:23 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
0
yes i know that window.open("example.com", "_blank", status=0); but what i mean is doing it by pressing a specific key from my keyboard so at the time i press the "y" key the url opens youtube in a new tab
22nd Oct 2017, 9:27 PM
Ilean
Ilean - avatar
0
you need to know key code of Y letter you can get it with this function: document.addEventListener('keydown', function(event){ alert(event.keycode) }
22nd Oct 2017, 9:34 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
0
thanks i"ll try that c:
22nd Oct 2017, 10:55 PM
Ilean
Ilean - avatar