Me loading an external script in a project in angular, through the file angular.json
"scripts": [
"src/assets/js/mask.js"
],
It is a script to apply a mask to some form fields. When the page is first loaded the script works, if I navigate to another page without refreshing and go back to the page containing the form it no longer works.
Is there another way to load this script whenever that component is accessed?
Remembering that the site works as a SPA (Single Page Application), so navigation between pages occurs without the need for new requests.
There are several masks, I run them through a function, see:
function id(el) {
return document.getElementById(el);
}
window.onload = function() {
id('nome')?.addEventListener("keyup", function() {
inicialMaiuscula(this);
});
id('telefone')?.addEventListener("keyup", function() {
mascara(this, mtel);
});
id('celular')?.addEventListener("keyup", function() {
mascara(this, mtel);
});
id('cep')?.addEventListener("keyup", function() {
mascara(this, cepm);
});
id('cep')?.addEventListener("change", function() {
getEndereco(this);
});
id('cpf')?.addEventListener("keyup", function() {
mascara(this, cpf_mask);
});
id('cpf')?.addEventListener("blur", function() {
validarCPF(this.value);
});
id('cpf')?.addEventListener("paste", function() {
return false;
});
id('rg')?.addEventListener("keyup", function() {
mascara(this, soNumeros);
});
id('nascimento')?.addEventListener("keyup", function() {
mascara(this, data);
});
id('usuario')?.addEventListener("keyup", function() {
letrasNumeros(event);
});
id('senha')?.addEventListener("blur", function() {
validaSenha(this);
});
id('confSenha')?.addEventListener("blur", function() {
validaSenha(this);
});
id('estado')?.addEventListener("input", function() {
tudoMaiuscula(event);
});
}
Full mask.js: https://jsfiddle.net/m50o6fn8/