Where to Put JavaScript Codes?

Most often, JavaScript is placed between the <head> and </head> section. However, that may not be practical for in some situations such as this single blog post. Fortunately, JavaScript may also be placed anywhere between the <body> and </body> tags. In fact, it’s known that placing .js codes closer to the bottom of the <body> section, right before the </body> tag, does speed up page loads. Here is a demo of this practice:

HTML Code:

<!DOCTYPE html>
<h2>JavaScript in Body</h2>
<p id="test">Click da button.</p>

<button type="button" onclick="myFunction()">Da Button</button>

function myFunction() {
document.getElementById("test").innerHTML = "Da Button has been pressed.";

Leave a Reply

Your email address will not be published. Required fields are marked *