Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you execute a Javascript function when the page has fully rendered?

How would you go about executing a Javascript function when the page is fully rendered (Displayed) I know that the onLoad event executes when the page is loaded but Before it's rendered.

I've previously solved this problem by using a timeout with a 0 interval, which seems to work usually, but seems a little inelegant to my delicate sensibilities.

Is there a proper way to do this?

UI'm using prototype as my java script framework so answers that involve that are helpful, however I would have thought that this would be built into Javascript.

like image 527
Omar Kooheji Avatar asked Jun 02 '09 13:06

Omar Kooheji


People also ask

How do I run a function after the page is loaded?

To get this event you should write following way: document. addEventListener('DOMContentLoaded', function() { // your code here }, false);

Can I run JavaScript before the whole page is loaded?

But yes, it is an option.

How do I execute a JavaScript function?

Use the keyword function followed by the name of the function. After the function name, open and close parentheses. After parenthesis, open and close curly braces. Within curly braces, write your lines of code.


1 Answers

The onload event fires when the page is fully rendered (including dependancies such as images).

Perhaps you are confusing it with Prototype's dom:loaded event (which fires when the DOM is loaded but before all the dependancies have been included)?

(To be honest, this sounds like an X-Y problem.)

like image 112
Quentin Avatar answered Nov 06 '22 09:11

Quentin