Home › JavaScript / Node / React › Simple JavaScript Called OnLoad HTML Example
JavaScript / Node / React · Dec 9, 2017 · 1 MIN READ

Simple JavaScript Called OnLoad HTML Example

To call a JavaScript function when an HTML page loads, use the code below:

[sourcecode language=”JavaScript”] <!DOCTYPE html>
<html>
<head>
<script>
function HelloWorld() {
document.write("Hello World");
}
</script>
</head>
<body onload="HelloWorld()">

</body>
</html>
[/sourcecode]

This displays:

And if using a DIV:

 
[sourcecode language=”JavaScript”] <!DOCTYPE html>
<html>
<head>
<script>
function HelloWorld() {
document.getElementById("MyDiv").innerHTML = "Hello World!";
}
</script>
</head>
<body onload="HelloWorld()">
<div id="MyDiv"></div>
</body>
</html>
[/sourcecode]

Which Displays:

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

Leave a Reply

Your email address will not be published. Required fields are marked *

Carl de Souza
KEEP LEARNING. KEEP BUILDING.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.