Home Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it
Reply: 0

Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

user11781
1#
user11781 Published in September 19, 2018, 3:18 am

This question already has an answer here:

  • $(document).ready equivalent without jQuery 32 answers

Okay, this might just be a silly question, though I'm sure there are plenty of other people asking the same question from time to time. Me, I just want to make 100% sure about it either way. With jQuery we all know the wonderful

$('document').ready(function(){});

However, let's say I want to run a function that is written in standard JavaScript with no library backing it, and that I want to launch a function as soon as the page is ready to handle it. What's the proper way to approach this?

I know I can do:

window.onload="myFunction()";

...or I can use the body tag:

<body onload="myFunction()">

...or I can even try at the bottom of the page after everything, but the end body or html tag like:

<script type="text/javascript">
   myFunction();
</script>

What is a cross-browser(old/new)-compliant method of issuing one or more functions in a manner like jQuery's $.ready()?

share|improve this question

marked as duplicate by thatOneGuy, rfornal, Matt Seymour, Juan Carlos Oropeza, Wilt Nov 16 '16 at 14:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    support for older browsers is nice, but with as fast as tech is moving forward and people seem to be catching up quicker these days its not 100% needed but a nice bonus if possible. Overall I am trying to figure what if any of these is the standard across browsers. Do they all work? Does it matter which I choose? If they all work what would be suggested as the best vs the rest? – chris Mar 28 '12 at 0:00
  • 7
    See this: stackoverflow.com/questions/799981/… – Dan A. Mar 28 '12 at 0:00
  • @clexmond putting script at the bottom vs top is something I've always understood for a variety of reasons. I tend to place it at the bottom, external files or on the page itself. But last couple years I've been spoiled by jQuery and other libraires. However I am now on a crusade of sorts trying to get a better understanding of javascript by itself with no lib backing it. So despite knowing what I know of javascript this simple little thing throws me off a bit as I want to make sure any script I do follows a compliant cross browser methodology. – chris Mar 28 '12 at 0:04
  • 4
    You could check out the source of jQuery to see how they implement the document ready function and work from there. – joshuahealy Mar 28 '12 at 0:04
  • @DanA. Nice. I think that sums the answer to this question up some. So JavaScript alone, there's no absolute way of easily finding a ready state, a means of sorts has to be created to support it across the various browsers. Thank you for finding that and clarifying it better for me. – chris Mar 28 '12 at 0:10

10 Answers 10

active oldest votes
up vote 1387 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.344171 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO