Gauls
Gauls

Reputation: 1965

window.onload function called twice

I have a simple form with post method. I am not sure why window.onload() function gets called twice below it shows up the page? Below is the code

  function window.onload() {        

       initialisePage();       

    }  
    <form id="frmMain" method="post" runat="Server"> 

</form>

I tried using document.isready function before calling initialisePage didn't help.

Upvotes: 0

Views: 8204

Answers (5)

Timothy C. Quinn
Timothy C. Quinn

Reputation: 4485

From the early 2000's I avoided using these types of hooks as they were undependable in these early days. Instead, I have always leaned on the tried and trued method of dropping in the 'init' call at the bottom of the page. It has never let me down.

    ...
    <script>
      body_loaded()
    </script>
  </body>
</html>

I know there are edge cases where this would not be ideal but it works 99.9% of usecases and follows 'KISS' principles.

Upvotes: 0

techloris_109
techloris_109

Reputation: 665

If window.onload gets called twice, means some changes are still happening on front side after initial load. A better to way would be by checking

window.onload = function() {
if(document.readyState == 'complete') {
myFunction();
}
};

Upvotes: 7

Brandon Smith
Brandon Smith

Reputation: 1197

<script>
window.onload = initialisePage;
</scipt>

Upvotes: 0

Tushar
Tushar

Reputation: 87203

SYNTAX ERROR in your code. onload should be written as follow:

<script>
window.onload = function() {
    initialisePage();       
}
</scipt>

OR

window.onload = initialisePage; // Thanks to @nnnnnn

Documentation

Upvotes: 1

Nilesh Chourasia
Nilesh Chourasia

Reputation: 408

You can use below line of code :

$(document).ready(function(){
    initialisePage();
})

Upvotes: 0

Related Questions