I'm executing an external script, using a <script>
inside <head>
.
Now since the script executes before the page has loaded, I can't access the <body>
, among other things. I'd like to execute some JavaScript after the document has been "loaded" (HTML fully downloaded and in-RAM). Are there any events that I can hook onto when my script executes, that will get triggered on page load?
Best method, recommended by Google also. :)
http://www.feedthebot.com/pagespeed/defer-loading-javascript.html
Using the YUI library (I love it):
Portable and beautiful! However, if you don't use YUI for other stuff (see its doc) I would say that it's not worth to use it.
N.B. : to use this code you need to import 2 scripts
Here's a script based on deferred js loading after the page is loaded,
Where do I place this?
What does it do?
Here's an example of the above code - Defer Rendering of JS
I wrote this based on defered loading of javascript pagespeed google concept and also sourced from this article Defer loading javascript
There is a very good documentation on How to detect if document has loaded using Javascript or Jquery.
Using the native Javascript this can be achieved
This can also be done inside the interval
Eg By Mozilla
Using Jquery To check only if DOM is ready
To check if all resources are loaded use window.load