Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running javascript after page is fully rendered

I am trying to create a syntax highlighter script. I tried using my script on a code with 10 thousand lines and all I see is a blank page while it is loading. Everything will just show up after the script has finished it's task. By the way, I called my script inside the ready function of jQuery.

$(myFunction); 

The script should execute after the page is fully rendered and the user can actually navigate through the page even if the script is not yet finished. The javascript will run in the background as it highlights the code one by one while not interfering the reponsiveness of the page. Thanks in advance.

EDIT:

To make this clearer, I would like to execute the code after everything is "rendered" not "loaded". Everything should already visible in the screen and the user can actually see the code come to life as it is being highlighted. Thanks.

like image 927
Randal Cunanan Avatar asked Dec 23 '11 02:12

Randal Cunanan


People also ask

How do I run JavaScript as soon as page loads?

If you are using an <script> inside <head> then use the onload attribute inside the <body> tag to Execute JavaScript after page load. It will run the function as soon as the webpage has been loaded.

Which event would you use to start a script after a page has been fully loaded by the browser?

The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

Can I run JavaScript before the whole page is loaded?

But yes, it is an option.


1 Answers

Do you mean after all the images have been loaded?

I think window.onload is what you're looking for

window.onload = function() {     //dom not only ready, but everything is loaded }; 

EDIT

Per Chris's comment, here's the jQuery way:

$(window).load(function() {     //dom not only ready, but everything is loaded }); 
like image 120
Adam Rackis Avatar answered Sep 22 '22 00:09

Adam Rackis