Sometime we need to find when the page is loaded or page is completely loaded before specific java script function is called. As we all know if try to access DOM element using document.getElementBtId() before page is loaded or page is completely loaded we get error. So to prevent the error we can user following java script to detect page is loaded?
1: var body = document.getElementsByTagName('BODY')[0];
2:
3: if (body && body.readyState == 'loaded') {
4: AfterLoad();
5: } else {
6: if (window.addEventListener) {
7: window.addEventListener('load', AfterLoad, false);
8: } else {
9: window.attachEvent('onload', AfterLoad);
10: }
11: }
12: function AfterLoad() {
13: alert("page loaded !!!");
14: }
Fig (1) – Detect Page is loaded !!
Happy Programming !!!
No comments:
Post a Comment