...

View Full Version : How to get document.ready using javascript



RajaRamu
09-05-2011, 04:22 AM
In Jquery, there are following two concepts

1. Document.ready
2. Window.onload

How can i get to know when document is ready to use using pure javascript?

DanInMa
09-05-2011, 04:30 AM
.onload IS the javascript method

RajaRamu
09-05-2011, 04:35 AM
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery demo</title>

</head>
<body>
<a href="http://jquery.com/">jQuery</a>
<script src="JQuery1.6.2.js"></script>
<script>
$(window).load(function(){
alert("Window load");
});
$(document).ready(function(){
alert ("Document ready");
});

</script>
</body>
</html>

Here "Document ready" will be painted first before even execute window load. So, My question is that without using jquey, if i want to know when document is ready to use. How can i find out? I appreciate your help!!

Logic Ali
09-05-2011, 05:01 AM
In Jquery, there are following two concepts

1. Document.ready
2. Window.onload

How can i get to know when document is ready to use using pure javascript?
Search for ondomready and you'll find several stand-alone solutions.

RajaRamu
09-05-2011, 05:17 AM
I can't find out using pure javascript. I do see lots of framework solutions. I want only in javascript. Can you please help me out on this?

DanInMa
09-05-2011, 06:24 AM
.load is a Jquery ajax function.
this is how you use onload



<html>
<head>
<script>
function foo(){
alert("bar")
}
window.onload = foo();
</script>
</head>
<body>
content
</body>
</html>

RajaRamu
09-05-2011, 06:51 AM
No. i have something to execute once DOM is ready before window.onload is being called.

This can be achieved in Jquery by

$(document).ready(function(){
alert ("Document ready");
});

i want the same functionality in javascript.

devnull69
09-05-2011, 07:05 AM
$(document).ready() fires on DOMContentLoaded, but this event is not being fired consistently among browsers. This is why jQuery will most probably implement some heavy workarounds to support all the browsers. And this will make it very difficult to "exactly" simulate the behaviour using plain Javascript (but not impossible of course).

In supporting browsers you can write something like this


document.addEventListener('DOMContentLoaded', function() {
// your code here
}, false);


For Internet Explorer and Safari you can follow this description (http://dean.edwards.name/weblog/2006/06/again/)

Arbitrator
09-05-2011, 07:44 AM
$(document).ready() fires on DOMContentLoaded, but this event is not being fired consistently among browsers.Using the below code, the DOMContentLoaded event is working fine for me in Chrome 14 Beta, Firefox 7 Beta 2, Internet Explorer 9, Opera 11.5, and Safari 5.1. (Of course, there may still be problems in older browsers.)


var d = document;
function document_loaded() {
d.defaultView.alert("The DOM is loaded!");
}
d.addEventListener("DOMContentLoaded", document_loaded, false);

(This should be no surprise though since the former Mozilla-only event has since become part of HTML5; see http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html and http://www.whatwg.org/specs/web-apps/current-work/multipage/section-index.html#events-0.)

RajaRamu
09-08-2011, 07:38 AM
Thank you all.This really helps!!

VIPStephan
09-08-2011, 11:36 AM
Searching for “DOM ready javascript” with Google gave me this as first result within one second: http://www.javascriptkit.com/dhtmltutors/domready.shtml



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum