We think pageLoad() and jQuery’s $(document).ready() events do the same. Both methods seem too similar in simple demo example. But $(document).ready() and pageLoad() methods are very much differ in functioning.In this article, I will explain the major differences between $(document).ready() and pageLoad() methods.
<script type="text/javascript">
$(document).ready(function(){
// Gets called as soon as DOM is ready
//code here
});
</script> pageLoad() method gets called when images and all associated resources of the page have been fully loaded. Suppose your web page has large size images then until all the images are not fully loaded on the page, pageLoad() method will not called. pageLoad() method is not browser compatible. We can have only one pageLoad() method on a web page.
<script type="text/javascript">
function pageLoad()
{
// gets called when page have been fully loaded
//code here
}
</script> Since we know, in asp.net update panel gets partially postback to the server. Hence If you are using $(document).ready() and pageLoad() methods on the page, it is mandatory to know the difference between both the methods.
pageLoad() methods is called each and every partial postback of update panel but $(document).ready() is not called each and every partial postback of update panel. $(document).ready() is called only one time (during first time of page loading). Hence code written in $(document).ready() method will not be initialized each and every partial postback.
<script type="text/javascript">
function pageLoad()
{
// code for initialization in each and every partial postback
}
$(document).ready(function(){
// code for one time initialization
});
</script>
<asp:ScriptManager ID="ScriptManger1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!-- Asp.net Controls Here -->
</ContentTemplate>
</asp:UpdatePanel> If you are using AJAX on your asp.net web page then you can use Application.add_init() method in place of $(document).ready() for one time initialization.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<script type="text/javascript">
Sys.Application.add_init(function() {
// Initialization code here, meant to run once.
});
</script> Note that to call Application.add_init, we need to place it after the ScriptManager. This is required because the ScriptManager injects its reference to MicrosoftAjax.js at that location. Attempting to reference the Sys object before Script Maanager will throw a javascript error "sys is undefined"
Best for onetime initialization.
Called as soon as DOM is ready; may called slightly before than pageLoad().
Cross browser compatible.
Unable to re-attach the functionality to elements/controls of the page affected by partial postbacks.
Not best for onetime initialization if used with UpdatePanel.
Not Cross browser compatible.
Best to re-attach the functionality to elements/controls of the page affected by partial postbacks with UpdatePanel.
Useful for one time initialization if only ASP.NET AJAX is available.
More work required to wire the event up.
"sys is undefined" javascript error may occurs if you are not careful.
In this article I try to expose document.ready() and window.onload/pageLoad(). I hope after reading this article you will be able to understand the use of both these methods. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.