Is there any way to write a function on the screen other than by onclick?

Asked

Viewed 189 times

3

Hello, the only way I know how to call a javascript function in html is by onclick, but my html is not a button to click, I wonder if there is another way to show a function when it is called.

ex: onload -> para quando a tela carregar
onclick -> quando clicarem no elemento

has some that just shows?

  • technically no, just that it appears... like the screen initialized it is there, but as the code is right in the middle.. I don’t know if I can use onload..

  • 1

    What do you mean "the code is right in the middle"? You can [Edit] and show the relevant chunk of your code?

  • What you want is to call a function when loading the page?

  • it’s in the middle, inside Ivs... the entire code is very extensive and what I need is right in the middle of it..

  • 1

    if it is a function, just run it like this: exibirDados()

  • Really? It really works... I don’t need to put into anything?

  • just need to be inside the script tag: <script>minhaFuncao();</script>

Show 2 more comments

3 answers

3


function fazerAlgo()
{
  //código aqui
}
// em alguma parte do seu código mais tarde...
fazerAlgo();

In HTML:

<script src="minhafuncao.js"></script>
<div id="">Em alguma parte do seu html</div>
<script>fazerAlgo();</script>
  • 1

    Simmm... Thank you... but how do I call this function in html?

  • she would look that way also in html?

  • In HTML you should put the script tag <script>fazerAlgo();</script> but to work you must call the script that contains the function before.

  • 1

    Just be careful that if the function references some element of html, it needs to exist at the time of the call. Therefore it is recommended to put the scripts just before the end of the <body>.

3

In your own question is one of the alternatives: no onload.

If you nay want to call the function by means of a direct action user (a click, for example), just call it form explicit, whether while loading the page, simply calling the function after the function code:

<script>
function minhaFuncao(){
...
}
minhaFuncao();
</script>

Or after page loading:

<script>
function minhaFuncao(){
...
}
window.onload = minhaFuncao;
</script>
  • Thank you very much ...it was very helpful to both answers, but I can only mark one as certain. I’m sorry I can’t mark yours either .... >-<

  • 1

    @Alis Yes, of course. The important thing is not to mark the answer as right, but to add some useful information. Good luck!

1

There are several listeners that can be used, follow some examples and forms of application with pure Javascript:

document.addEventListener('click', suaFuncao(), true); // No Clique
document.addEventListener('load', suaFuncao(), true); // No load
document.addEventListener('resize', suaFuncao(), true); // No Resize

and to apply them we assume, in the click:

const btn = document.querySelector('classe ou id do seu botao');
btn.addEventListener('click', suaFuncao(), true);
  • You are running the functions and passing the returns instead of the references.

  • @bfavaretto can explain to me better what you said?

  • I meant it would have to be document.addEventListener('click', suaFuncao, true);, without parentheses after function name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.