How to Run Script Again Web Page

How to run a function when the folio is loaded in JavaScript ?

A function can be executed when the page loaded successfully. This can be used for diverse purposes like checking for cookies or setting the correct version of the folio depending on the user browser.

Method i: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload issue occurs whenever the chemical element has finished loading. This can be used with the body element to execute a script later on the webpage has completely loaded. The function that is required to be executed is given here.

Syntax:

<trunk onload="functionToBeExecuted">

Example:

<!DOCTYPE html>

< html >

< head >

< title >

How to run a function when the

page is loaded in javascript ?

</ championship >

</ head >

< body onload = "console.log('The Script volition load now.')" >

< h1 fashion = "color: greenish" >GeeksforGeeks</ h1 >

< b >

How to run a function when the

page is loaded in javascript ?

</ b >

< p >

The script has been executed. Check

the console for the output.

</ p >

</ body >

</ html >

Output:
body-onload-output
Panel Output:
body-onload-console

Method 2: The window object represents the browser window. The onload property processes load events after the chemical element has finished loading. This is used with the window element to execute a script after the webpage has completely loaded. The function that is required to be executed is assigned as the handler function to this holding. It will run the role as soon as the webpage has been loaded.

Syntax:

window.onload = function exampleFunction() {

}

Example:

<!DOCTYPE html>

< html >

< head >

< title >

How to run a function when the

page is loaded in javascript ?

</ championship >

</ caput >

< body >

< h1 style = "color: dark-green" >

GeeksforGeeks

</ h1 >

< b >

How to run a part when the

page is loaded in javascript?

</ b >

< p >

The script has been executed. Check

the console for the output.

</ p >

< script >

window.onload = function exampleFunction() {

console.log('The Script will load now.');

}

</ script >

</ torso >

</ html >

Output:
window-onload-output
Console Output:
window-onload-console

JavaScript is best known for web page development but it is besides used in a diversity of non-browser environments. Y'all can acquire JavaScript from the ground upwardly past post-obit this JavaScript Tutorial and JavaScript Examples.


christensonefrely87.blogspot.com

Source: https://www.geeksforgeeks.org/how-to-run-a-function-when-the-page-is-loaded-in-javascript/

0 Response to "How to Run Script Again Web Page"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel