I can create a function in JavaScript and call it as follows:
funcaoTeste = function()
{
console.log('Ola funcao teste');
};
funcaoTeste();
To execute the function I need to call it funcaoTeste()
, entertaining, sometimes I run into JavaScript codes where a function is executed but I do not see where it is called.
See this handy illustration example:
window.onload = function()
{
document.getElementById('form-exemplo').onsubmit = function (event)
{
if ( CPF.validate(document.getElementById('cpf').value) !== true )
{
alert("CPF invalido");
return false;
}
};
};
The code above refers to this form:
<form method="post" id="form-exemplo">
<label for="nome">Nome </label>
<input type="text" id="nome" placeholder="Digite o nome" required>
<label for="cep">CEP </label>
<input type="text" id="cep" placeholder="Digite o CEP" required pattern="\d{5}-?\d{3}">
<label for="cpf">CPF </label>
<input type="text" id="cpf" placeholder="Digite o CPF" required>
<input type="submit" value="Enviar">
</form>
Note that the onload()
and onsubmit()
functions have been declared, but there is no call to them in script . It seems that this occurs in an automatic way that I do not know.
Questions
onload()
and
onsubmit()
?