0

I want to run window.alert("hi") every time I load a specific URL.

For example: The page will say "hi" automatic without calling a javascript function.

Ani Menon
  • 27,209
  • 16
  • 105
  • 126

1 Answers1

1

Put it in a function & call it on-load(document/window) or just put the js within script tag on the page(it would work as soon as that part is loaded).

Example using onload (this alerts "Page is loaded" as soon as the body loads):

<html>
<body onload="myFunction()">

<h1>Hello World!</h1>

<script>
function myFunction() {
    alert("Page is loaded");
}
</script>

</body>
</html>

Example (by adding script between the HTML):

<html>

<body>
  <script>
    alert("Page is loaded");
  </script>

  <h1>Hello World!</h1>

</body>

</html>

Using window.onload(THis loads as soon as the window is done loading) :

<html>

<head>
  <script>
    window.onload = function(e) {
      alert("Hello window is loaded");
    };
  </script>
</head>

<body>
  <h1>Hello World!</h1>
</body>

</html>

Reference : window.onload vs document.onload

Community
  • 1
  • 1
Ani Menon
  • 27,209
  • 16
  • 105
  • 126