Salve, sto studiando sviluppo web e volevo chiedervi se separare anche il codice js degli eventi, per esempio onclick, onsubmit, ... da quello html è una buona cosa o no.
Questo è il codice che sto utilizzando, volevo anche chiedere perché se invece di usare "window.addEventListener("load"..." uso "window.addEventListener("DOMContentLoaded"...", quando apro la pagina mi dice che ajaxRequest non è definita? Dal debug se vado a vedere nell'head lo script è stato caricato.
Codice:
var script = document.createElement('script');
script.src = 'script/ajax_request.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
window.addEventListener("load", function(event) {
var form = document.getElementsByTagName("form")[0];
form.addEventListener("submit", function (event) {
validate_input(event);
}, false);
checkJS();
ajaxRequest("request.php", "state=italia", "states");
});
function checkJS() {
document.getElementById("JS").style.display = "inline";
}