2

Possible Duplicate:
$(document).ready equivalent without jQuery

I have a script I need to write that needs to execute on DOM ready, but I can't have dependency on JQuery as part of the script.

I want to imitate $(document).ready(), How can I implement this behavior in the shortest way?

Community
  • 1
  • 1
David MZ
  • 3,329
  • 5
  • 31
  • 47
  • 1
    Karoly was not joking. If you really want it to function like jQuery, just use that part of the source code. – rkw Mar 20 '12 at 09:46
  • I did copy the JQuery function I seems to be the best solution, thank you I just wasn't sure – David MZ Mar 20 '12 at 16:17

3 Answers3

3

You can use:

document.addEventListener("DOMContentLoaded", function(){
//do things
},false);

for example. Note that this might be a problem in older browsers though, see MDN for a list of supported browsers.

m90
  • 10,334
  • 11
  • 53
  • 105
0

You could do 2 things. On the body tag add an onload event:

    <body onload="myJSFunction()"></body>

Otherwise, you can add the functions in the bottom of the markup with:

<script type="text/javascript"> function myFunction()</script>
Th0rndike
  • 3,360
  • 3
  • 19
  • 39
-1
window.onload = function() {
    // write some javascript here
    // this will be executed on page load
}
tusar
  • 3,304
  • 6
  • 33
  • 56