Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a javascript function as last inside a page

I need to be sure that a certain script within a page is executed as last. I thought of using JQuery

$(document).ready( ... )

but if there are more functions of this type, which is actually executed last?

like image 619
rvandoni Avatar asked Aug 28 '12 08:08

rvandoni


People also ask

How do you call a function on page load?

The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.

How do you execute a function when a page is fully loaded?

Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded.

How can I execute a JavaScript function on the first page load?

To execute a JavaScript function on the first page load, we can set a flag in local storage is done. Then we stop running the function on subsequent page loads if the flag exists. to set window. onload to a function that checks if the local storage with key 'hasCodeRunBefor' exists.

How to execute function after complete page load with JavaScript?

To execute function after complete page load with JavaScript, we listen to the readystatechange event. document.addEventListener ("readystatechange", (event) => { if (event.target.readyState === "interactive") { console.log ("hi"); } if (event.target.readyState === "complete") { console.log ("hi 2"); } });

When does the code inside a JavaScript function execute?

The code inside a JavaScript function will execute when "something" invokes it. The code inside a function is not executed when the function is defined. The code inside a function is executed when the function is invoked.

What is a JavaScript function?

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). Example

What happens when JavaScript reaches a return statement?

When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.


1 Answers

There are many ways to delay the execution of a script.

There is no way to programatically detect all of them.

For a reliable solution you would have to reverse engine the code of each page you care about, figure out what it is doing (and when) and write your delay script specifically for that page. (For a value of "reliable" equal to "until they change the page").

like image 80
Quentin Avatar answered Nov 12 '22 00:11

Quentin