I have a code, which avoids showing the alert when the input is in focus:
var focused = false;
window.addEventListener("DOMContentLoaded",()=>{
var inputs = document.getElementsByTagName("INPUT");
for(var input of inputs){
input.addEventListener("focus",()=>{
focused = true;
});
}
redirect();
});
function redirect(){
window.addEventListener("keyup", e => {
if((e.key === "y" || e.key == "Y") && !focused) alert("Ir a youtube?");
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="text" placeholder="Escribime aqui"/>
</body>
</html>
The code works well, the problem is that by modifying the redirect () function and leaving the focused variable in a single conditional, the code does not work, like this:
var focused = false;
window.addEventListener("DOMContentLoaded",()=>{
var inputs = document.getElementsByTagName("INPUT");
for(var input of inputs){
input.addEventListener("focus",()=>{
focused = true;
});
}
redirect();
});
function redirect(){
if(!focused){
window.addEventListener("keyup", e => {
if(e.key === "y" || e.key == "Y") alert("Ir a youtube?");
});
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="text" placeholder="Escribime aqui"/>
</body>
</html>