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>