Javascript: how to load all html page code

should the program call the function after loading all the code, including HTML, javascript, CSS, etc.? Can javascript do this?

+4
source share
6 answers

for javascript

window.onload = function(){ //your code }; 

for jquery

 $(document).ready(function(){ //your code }); 
+5
source

window.onload will light up after all images, frames and objects have finished loading onto the page. Your question is not clear enough about whether you want the script to wait for them, but if you do not, you will need a “finished document”.

First, many (all?) DOM-based Javascript frameworks provide this functionality, a cross-browser in the form of an event. jQuery example:

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

If you want to do this without a frame, it becomes a little more uncomfortable. Most browsers (coughnotIE) provide a DOMContentLoaded event:

 if (document.addEventListener) { document.addEventListener('DOMContentLoaded', function () { alert("DOM is ready"); }, false); } 

For the IE part, this is the defer job in the script tag. You can use conditional comments to make sure that only IE parses the script:

 <!--[if IE] <script type="text/javascript" defer> alert("DOM is ready"); </script> <![endif]--> 
+3
source

If you use the jQuery library, you just do this:

 $(document).ready(function() { // The code you need to have executed after loading the page }); 
0
source
 window.onload = function() { // Your code here }; 

What have you tried?

0
source

You can use <body onload="doStuff()"> , or you can use window.onload in your script. Check this out.

0
source

The jQuery $ (document) .ready (...) method is launched when the dom is loaded and can be controlled, and before all scripts, images, etc. are loaded.

The window.onload event will fire when everything that was requested completes the download.

0
source

Source: https://habr.com/ru/post/1312666/


All Articles