Computer >> Computer tutorials >  >> Programming >> Javascript

How do I call a JavaScript function on page load?


To call a function on page load, use −

window.onload

Example

You can try to run the following code to implement a JavaScript function on page load

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         function alertFunction() {
            alert('ok');
         }
         window.onload = alertFunction;
      </script>
   </body>
</html>