In an external js file, I have code for these two Go-To-Home buttons.
One for …/index.html and the other for …/…/index.html
Several pages will link to it:
const btnReturnToHome = document.getElementById("returnToHome");
function returnToHome(){
console.log("go to Home");
window.location=("../index.html");
}
btnReturnToHome.addEventListener('click', returnToHome);
const btnReturnToHome2 = document.getElementById("returnToHome2");
function returnToHome2(){
console.log("go to Home2");
window.location=("../../index.html");
}
btnReturnToHome2.addEventListener('click', returnToHome2);
Is it OK to have the code like this? The buttons, if put on the same html page, will work:
Both of these buttons will work, taking me back.
But if I use only one button, then the remaining button will not work. Nothing happens on click.
When I keep both, but put a style display:none on one, then the other works:
This display:none workaround is a hack.
What’s the right way to code these in the external file?
Above both of those scripts is a (working) Return-To-Top button script. Not sure if it is the cause of the confusing behavior:
Like @Archibald says, it’s because the button doesn’t exist. When it hits that line of code, the JavaScript will fail because the object doesn’t exist to add the listener to. Let me guess, you removed btnReturnToHome and tried to use just btnReturnToHome2?
The if (btnReturnToHome) check in the code he provides checks for the existence of an object prior to trying to do something with it.
What you’re saying @DaveMaxwell what you’re saying is that if the button isn’t there, then ALL the rest of the JavaScript fails on the page. Ouch. I get it now. That explains why moving a bit of code from below those lines to above had it working again.
@Gandalf I happened to have used an input button in this case, so I needed JS to make it work. You’re right; that would have been an easier way and saved more time. <a href=""><button>
But the gift in the end was learning a little bit more JS!
Not quite…since it’s not compiled, JS stops processing on the first error, but will run everything up to that point will run.
So if you were to remove ReturntoHome2, the JS for ReturntoHome will still work because that block of code hasn’t been executed yet. When you removed ReturnToHome, the JS for ReturntoHome2 doesn’t get executed because the JS for ReturntoHome has executed and failed.
Here’s an example based off your initial code earlier. Click on the three buttons and see how they behave.
The 2a was intentional to cause the JS to break…
Yeah, it’s odd. Sometimes it works but other times it doesn’t. What’s supposed to happen is the first button is supposed to fire the alert but the other two don’t. Dunno if it’s because it’s on codepen or what the deal is, but that was the intent (and it was doing it earlier when I linked it)
It throws the following console error when viewing outside of codepen. Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.