I have to websites index.html and index.php and both pages contain a javascript file script.js.
I'd like to execute some code from script.js to index.html and other to index.php.
Is it possible to do this?
JS Code
function ChangeColor()
{
button.addEventListener("mouseover", () =>
{
button.classList.remove("red");
button.classList.add("blue");
}
)
button.addEventListener("mouseout", () =>
{
button.classList.add("blue");
button.classList.remove("red");
}
)
}
function CheckErrors()
{
//Content
}
In My previous code, I want execute function ChangeColor in both websites because I have a button, hovewer my second function I want execute it just in index.html because I have a form.
How can I do this?, maybe with window object??
You can check whether location.pathname is equal to /index.html:
function ChangeColor() {
button.addEventListener("mouseover", () => {
button.classList.remove("red");
button.classList.add("blue");
})
button.addEventListener("mouseout", () => {
button.classList.add("blue");
button.classList.remove("red");
})
}
if (location.pathname == "/index.html") {
function CheckErrors() {
//Content
}
CheckErrors();
}