Run JS when opening the page

Asked

Viewed 15,503 times

1

Good evening. I have a js where I need it executed when loading the page. It works when I click, but I cannot make the calculation automatically when loading the page.

$(document).ready(function(){
    executa5();
    $(".cubagem, .tabbable").on('click mouseover load', executa5);

        function executa5(){
            valorTotaldaNota    = $("#valorTotaldaNota").val();
            valorTotaldaNota    = valorTotaldaNota.replace(".", "");
            valorTotaldaNota    = valorTotaldaNota.replace(",", ".");
            adValorem           = $("#adValorem").val();
            gris                = $("#gris").val();

            v1   = valorTotaldaNota * adValorem / 100;
            v2   = valorTotaldaNota * gris / 100;
            v1v2 = v1+v2;

            $("#valordoSeguro").val( v1v2.toFixed(2) );
    };  
});

What am I doing wrong?

  • verified the element inspector to see if no error occurred?

  • declares its function executa5 out of the . ready

  • @Maiconcarraro Put this as an answer

  • is just a suggestion, if you solve I turn in response :)

  • I did, but it didn’t work.

  • Did you do what William said? Probably your job is making a mistake and you’re stopping

  • As I check on the inspector to see if there are any mistakes?

  • Right-click, inspect element, will open a stops looking for the console tab and analyzes errors.

  • There is no mistake appearing in the inspector

Show 4 more comments

2 answers

1

James, there’s nothing wrong with your logic (http://jsfiddle.net/felipe_douradinho/8mdky0zk/1/)

Another code is preventing completion (i.e., the function is stored in memory but the document is not ready).

To debug using the inspector, go to the tab Sources, choose the document containing javascript and add a break point in the:

$(document).ready(function(){
executa5(); // <-- adicione o break point aqui

Then refresh the page to see if the executed call5() is passed on the upload. If it’s not, enter the break point at another time in javascript and check successively where the code no longer passes. Step by step typing F11 and F10.

inserir a descrição da imagem aqui

  • No error appearing on the console

  • It is not necessary an error to prevent JQUERY interpreting the DOM completion...it could save in JSFIDDLE for us to see?

  • Save what in JSFIDDLE ?

0

Are you using which version of jQuery?

The method on only added in version 1.7+. Make sure you are importing the correct version.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

Example:

function executa5(){
  alert("entrou");
  valorTotaldaNota    = $("#valorTotaldaNota").val();
  valorTotaldaNota    = valorTotaldaNota.replace(".", "");
  valorTotaldaNota    = valorTotaldaNota.replace(",", ".");
  adValorem           = $("#adValorem").val();
  gris                = $("#gris").val();

  v1   = valorTotaldaNota * adValorem / 100;
  v2   = valorTotaldaNota * gris / 100;
  v1v2 = v1+v2;

  $("#valordoSeguro").val( v1v2.toFixed(2) );
}  

$(document).ready(function(){
    executa5();
    $(".cubagem, .tabbable").on('click mouseover load', executa5);
});
.cubagem {
  height: 50px;
  width: 50px;
  background-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<div class="cubagem">
</div>

<input id="valorTotaldaNota" value="100" /> <br/>
<input id="adValorem" value="5" /> <br/>
<input id="gris" value="20" /> <br/>
<input id="valordoSeguro" />

  • My jQuery version is 2.0.3

  • @James Show me how you are caring

  • As so friend, importing?

  • How you are calling the jQuery js file?

  • This at the end of php file

  • Before or after your $(document).ready(function...?

  • Appears after.

  • Put before him

  • Nothing, not the same thing. I use DW, there is some program or extension to debug and try to see where the error is?

  • Glue me the line you add jQuery to the site

  • <src='Assets/js/jquery-2.0.3.min. js'>

  • Two things, check whether in the same directory as your page is the directory that has the Assets folder. And close the </script tag>

  • It’s closed. Everything works in the system. Except what I’m trying to do now. Strange this!

  • Checked if in the same directory as your page is the directory that has the Assets folder?

  • Yes, they’re in the same directory

Show 10 more comments

Browser other questions tagged

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