Tengo sitios web index.html e index.php y ambas páginas contienen un archivo javascript script.js .
Me gustaría ejecutar un código de script.js a index.html y otro a index.php .
¿Es posible hacer esto?
Código JS
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 }En mi código anterior, quiero ejecutar la función ChangeColor en ambos sitios web porque tengo un botón, sin embargo, mi segunda función quiero ejecutarla solo en index.html porque tengo un formulario.
¿Cómo puedo hacer esto? ¿Quizás con un objeto de ventana?
Puede verificar si location.pathname es igual a /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(); }