7
I can create a Javascript function and call it as follows:
funcaoTeste = function()
{
console.log('Ola funcao teste');
};
funcaoTeste();
To perform the function I need to call her funcaoTeste()
, however, sometimes I come across Javascript codes in which a function runs but I don’t see where it’s called.
See this practical 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 functions onload()
and onsubmit()
have been declared, but there is no call for them in the script. It seems that this occurs in an automatic way that I do not know.
Questions
- How a function is executed automatically without being called?
- Who is responsible for carrying out the tasks
onload()
andonsubmit()
?
Are performed at events
load
andsubmit
respectively.– Maurivan