Reputation: 93
I want a way to know if a particular web page is completely loaded using javascript
Ex) A web page can have dynamic contents getting displayed through Ajax, or contents comming through javascript, all the images and css.I need to find out when all these contents are loaded properly and the webpage is stablilized.
I got some pointers like ondocumentready its an alternate soultion for window.onload, onreadystatechange but these solutions are unreliable sometimes it works and sometimes it doesn’t
Also the solution should be browser independent, i.e it should work for all browsers
Any pointers on this will be highly appreciated
Thanks in advance
Upvotes: 2
Views: 334
Reputation: 38307
As a comparison of both answers by @Rigobert and @cottsack:
window.onload
:
should be used only to detect a fully-loaded page.
So it should be a good fit for your question.
Whereas $(document).ready(
fires (in most cases, browser-dependent)
when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
See also window-onload-vs-document-ready and the DOMContentLoaded event (which seemingly triggers JQuery's $(document).ready()). jquery-ready-vs-window-onload has a nice answer, too.
Upvotes: 0
Reputation: 2804
Using pure javascript you can put javascript in the onload event of the body tag like so:
<body onload="function">
This will trigger when the page has finished loading.
Upvotes: 1
Reputation: 187110
You can also use the following. It's a shorthand version of the code posted by @cottsak
$(function()
{
// your code
});
if you’re using jQuery.noConflict() (which is used to give control of the $ variable back to whichever library first implemented it), you can do this:
By adding jQuery.noConflict(), jquery won't conflict with $ variables of other libraries
jQuery(function($)
{
// your code
});
Upvotes: 1
Reputation: 11535
Use jQuery and its easy:
<script type="text/javascript">
$(document).ready(function() {
alert('doc is ready');
});
});
</script>
Upvotes: 4