Cara menggunakan javascript init function onload

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

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. The function that is required to be executed is given here.

Syntax:

<body onload="functionToBeExecuted">

Example:




<!DOCTYPE html>

<html>

  

<head>

    <<!DOCTYPE html>1>

<!DOCTYPE html>3<!DOCTYPE html>4

<!DOCTYPE html>3<!DOCTYPE html>6

    <!DOCTYPE html>8<!DOCTYPE html>1>

<!DOCTYPE html>8head>

  

<<6 <7<8<9>

html

    <html4 html5<8html7html8html4>

html

    <>4>

<!DOCTYPE html>3<!DOCTYPE html>4

<!DOCTYPE html>3<!DOCTYPE html>6

    <!DOCTYPE html>8>4>

html

    < 7>

<!DOCTYPE html>3<0

<!DOCTYPE html>3<2

    <!DOCTYPE html>8 7>

<!DOCTYPE html>8<6>

  

<!DOCTYPE html>8html>

Output:

Cara menggunakan javascript init function onload

Console Output:
Cara menggunakan javascript init function onload

Method 2: The window object represents the browser window. The onload property processes load events after the 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 property. It will run the function as soon as the webpage has been loaded.

Syntax:




head4head5 head6

  

    head9

>0

Example:




<!DOCTYPE html>

<html>

  

<head>

    <<!DOCTYPE html>1>

<!DOCTYPE html>3<!DOCTYPE html>4

<!DOCTYPE html>3    6

    <!DOCTYPE html>8<!DOCTYPE html>1>

<!DOCTYPE html>8head>

  

<<6>

    <html4 html5<8html7>

<!DOCTYPE html>3<!DOCTYPE html>16

    <!DOCTYPE html>8html4>

html

    <>4>

<!DOCTYPE html>3<!DOCTYPE html>4

<!DOCTYPE html>3<!DOCTYPE html>29

    <!DOCTYPE html>8>4>

html

    < 7>

<!DOCTYPE html>3<0

<!DOCTYPE html>3<2

    <!DOCTYPE html>8 7>

html

    <<!DOCTYPE html>50>

<!DOCTYPE html>3<!DOCTYPE html>53

<!DOCTYPE html>54<!DOCTYPE html>55

<!DOCTYPE html>3>0

    <!DOCTYPE html>8<!DOCTYPE html>50>

<!DOCTYPE html>8<6>

  

<!DOCTYPE html>8html>

Output:

Cara menggunakan javascript init function onload

Console Output:
Cara menggunakan javascript init function onload

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.