Lets say i have 2 html pages ( home & about ), inside the home page i have a header like so
<header id="#header">
some code
</header>
and this header is only on the home page so if i selected this header in java script main file
const header = document.getElementById('#header');
it's going to give me an error on about page because there is no such a thing as header in this page, so how do you prevent those kind of errors ? do I have to make it a local variable instead ?
what I actually made is something like this
if( body.className = 'home' ) {
some code
}
like this I will be sure that about page won't have access to anything that related to the home page, but is this is a bad practice ?