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.
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.
Share Improve this question asked Jun 29, 2015 at 11:38 GaulsGauls 1,9656 gold badges29 silver badges44 bronze badges 3- 2 I don't see how it could be called at all given that if it is JS you have a syntax error in the declaration. Is there server-side stuff going on there too? Are you sure you've used the right tags on your question? – nnnnnn Commented Jun 29, 2015 at 11:40
- what is the <form> post's relation with the onload function? – Devjit Commented Jun 29, 2015 at 11:57
- 1 The javascript code sits within script tag "<script language="Javascript"> . it is with an aspx file. The problem is not why it is not called but it gets called twice? So there isn't any syntax issue in the code. – Gauls Commented Jun 29, 2015 at 14:17
5 Answers
Reset to default 7If 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 == 'plete') {
myFunction();
}
};
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
You can use below line of code :
$(document).ready(function(){
initialisePage();
})
<script>
window.onload = initialisePage;
</scipt>
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.