Lalchand
Lalchand

Reputation: 7827

Calling JavaScript function before the end of body

How do I call a JavaScript function before the end of the body of an HTML page?

Upvotes: 0

Views: 3428

Answers (2)

Nick Craver
Nick Craver

Reputation: 630637

You can just place a <script> block in there, like this:

  <script type="text/javascript">
    myFunction();
  </script>
</body>

Also, if you're using a framework of some sort, most of them have handlers for when the DOM is ready, or you can use window.onload, whatever best fits your situation.

Upvotes: 3

bezmax
bezmax

Reputation: 26152

<html>
<head>
<script...>
    function helloWorld() {
        alert('Hello world!');
    }
</script>
</head>
<body>
...some content...
    <script...>
        helloWorld();
    </script>
</body>
</html>

Upvotes: 2

Related Questions