1

How to add myFunction() in head that runs only after the body has been loaded. The reason I want to have it inside head is because that way I could put it in only one file instead of multiple.

Currently I have <body onload="myFunction()"></body> which works perfectly. But this needs to be added to multiple files.

Example

<head>
  <style>
    .codetext {
      font-family: monospace;
    }
  </style>

  <script>
    myFunction = function () {
      let links = document.querySelectorAll("a");
      links.forEach(elem => elem.classList.add("codetext"));
    }
 document.body.onload("myFunction"); <!--Doesn't Work-->
  </script>
</head>

<body>
  <a href="">Click Here</a>
</body>
d.b
  • 29,772
  • 5
  • 24
  • 63
  • 3
    This question may already have been answered here: https://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery – Snowmonkey Feb 24 '19 at 19:34
  • 1
    Why is `body.onload` used instead of `window.onload`? `` – guest271314 Feb 24 '19 at 19:36

5 Answers5

2

You can use global load event

<script>
  onload = _ => 
    document.querySelectorAll("a").forEach(elem => elem.classList.add("codetext"))
</script>
guest271314
  • 1
  • 10
  • 82
  • 156
1

Use document.body.onload = handler. See this answer for more details.

<head>
  <style>
    .codetext {
      font-family: monospace;
    }
  </style>

  <script>
    document.body.onload = () => {
      const links = document.querySelectorAll('a');
      links.forEach(elem => elem.classList.add('codetext'));
    }
  </script>
</head>

<body>
  <a href="">Click Here</a>
</body>

Or you can use window.onload = handler.

jo_va
  • 11,779
  • 3
  • 20
  • 39
1

You can use

 document.querySelector('body').onload

<head>
  <style>
    .codetext {
      font-family: monospace;
    }
  </style>

  <script>
    document.querySelector('body').onload= function myFunction() {
      let links = document.querySelectorAll("a");
      links.forEach(elem => elem.classList.add("codetext"));
    }
 //document.body.onload("myFunction"); <!--Doesn't Work-->
  </script>
</head>

<body>
  <a href="">Click Here</a>
</body>
ellipsis
  • 11,498
  • 2
  • 13
  • 33
1

TypeError: document.body.onload is not a function

you need to assign your function to onload : document.body.onload = myFunction;

<head>
  <style>
    .codetext {
      font-family: monospace;
      color: red;
    }
  </style>

  <script>
    myFunction = function() {
      let links = document.querySelectorAll("a");
      links.forEach(elem => elem.classList.add("codetext"));
    }
    document.body.onload = myFunction;
  </script>
</head>

<body>
  <a href="">Click Here</a>
</body>
Taki
  • 15,354
  • 3
  • 20
  • 39
0

Use

document.body.addEventListener("load",functionToAssingn)

to add onLoad event:

<head>
  <style>
    .codetext {
      font-family: monospace;
    }
  </style>

  <script>document.body.addEventListener("load",myFunction)
    myFunction = function () {
      let links = document.querySelectorAll("a");
      links.forEach(elem => elem.classList.add("codetext"));
    }
  </script>
</head>

<body>
  <a href="">Click Here</a>
</body>
FZs
  • 11,931
  • 11
  • 28
  • 41