I’m trying to make a navigation system using document.onkeyup where pressing left arrow on the keyboard forwards the user to the previous ID and the same with the right arrow forwarding to the next ID.
I’d also like to use the R key to bring people to a random ID, however there is also a search and login form so when the user presses R, while typing, they will be forwarded to another page.
How can I avoid this?
here’s my code
$AddToSite_JavaScript = '<script>
document.onkeyup=checkKeycode;
function checkKeycode(e)
{
var keycode;
if(window.event) keycode=window.event.keyCode;
else if(e) keycode=e.which;
if(keycode=="37") window.location.href=\''. $WebsiteRoot ."/". $PreviousId ."-". $PreviousTitle .'\';
else if(keycode=="39") window.location.href=\''. $WebsiteRoot ."/". $NextId ."-". $NextTitle .'\';
else if(keycode=="13" || keycode=="82") window.location.href=\''. $WebsiteRoot ."/random" .'\';
}
</script>';
If you are only using inputs for text: throwing the following code after
keycode=="82"will do the trick:&& e.srcElement.tagName != "INPUT"Essentially, it just checks if the key stroke originated from an input box.
If you don’t want pressing enter in an input to trigger it: put your existing code in () and then add this.