Error calling 2 javascript functions at the same time

Asked

Viewed 796 times

0

Hello, I need to call these two separate functions as this only one code works as I do for both work?

<script type="text/javascript">

    window.onload = function() {

        if ('a' == 'a')

        {
           document.getElementById("nomeprodut").innerHTML = "Altera nome do produto";
        } 

    }
</script>

<div id="nomeprodut"></div>


<script type="text/javascript">

    window.onload = function() {

        if ('b' == 'b')

        {
           document.getElementById("b").innerHTML = "grghgh";
        } 

    }
</script>

<div id="b"></div>
  • 1

    I may be wrong, but the onload event only occurs 1 time, and so only calls the last function that defined as onload, if I’m not mistaken the last one overwrites the previous one. Without putting everything in the same onload or at least in separate functions I think there is no way.

  • @Neuberoliveira Concerteza should be the same problem, the page generates each separate javascript, would have as I do a function equal to this without onload sera?

  • 1

    Some special reason to be carrying several Jss with an onload in each one, there is no way to move everything to one place only?

  • 1

    @Josimara asked a recent question about onload, maybe the other function can help you!

  • @Neuberoliveira I have a page that generates a list of products, each product generates a javascript, I can only use html/javascript

  • @Marconi That’s just what I needed, Thanks...

  • 1

    @Good Josimara I could help :)

Show 2 more comments

3 answers

2

You can add more than one Function to the same event, and it will run once in the case of onload, but will perform all functions in the order you add using addEventListener. Documentation here: https://www.w3schools.com/js/js_htmldom_eventlistener.asp

Basically, you do the following:

window.addEventListener("load",function(event) {
    if ('a' == 'a') {
    document.getElementById("nomeprodut").innerHTML = "Altera nome do produto";
  }
},false);

and then:

window.addEventListener("load",function(event) {
  if ('b' == 'b') {
    document.getElementById("b").innerHTML = "grghgh"; 
  }
},false);

And so on, you can add other systems to the load event.

2

I suggest using the Domcontentloaded event, which runs after all content is loaded. See an example:

<script type="text/javascript">
    function a() {
        alert('a');
        if ('a' == 'a')
        {
           document.getElementById("nomeprodut").innerHTML = "Altera nome do produto";
        } 
    }
</script>
<div id="nomeprodut"></div>

<script type="text/javascript">

    function b() {
        alert('b');
        if ('b' == 'b')

        {
           document.getElementById("b").innerHTML = "grghgh";
        } 
    }

document.addEventListener('DOMContentLoaded', function(){
    a();
    b();
});

</script>
<div id="b"></div>

1

Put it all inside a onload only, thus remaining:

<div id="nomeprodut"></div>
<div id="b"></div>

<script type="text/javascript">
window.onload = function () {
  if ('a' == 'a') {
    document.getElementById("nomeprodut").innerHTML = "Altera nome do produto";
  }
  if ('b' == 'b') {
    document.getElementById("b").innerHTML = "grghgh"; 
  }
}
</script>
  • The problem is that the page generates javascript for each product on my site so need separate, or have some way to do without onload?

  • 1

    One solution would be not to use the onload, However, you may run the risk of the script running even before the HTML rendering, which would cause errors, because you can’t find the element on the page. But you have to test to see. Another way is to use the setTimeout Before changing the content of page elements, it is a "not elegant" solution, but one that can work. Another point, however, is to verify the need to generate Javascript code for each product on the page and to think about other ways to do this, something more global than product specific.

  • The problem is that I can only do this function using html/js, if php would not have this problem, I will try setTimeout, putting only the function without onload does not work the code

Browser other questions tagged

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