JavaScript > Placing JavaScript code

Code placement in JavaScript

Where to keep JavaScript code in HTML page?

Ideally, JavaScript codes should be kept under <head></head> tag and called from <body> parts of the page, however JavaScript code can be kept anywhere in the page under <script> tag.

<!DOCTYPE html>
        function AlertMe() {
            alert("I am called from JavaScript!");
    <input type="button" id="btnTest" onclick="AlertMe()" value="Click me!" />

Above code snippet shall show an alert message when “Click me!” button will be clicked.

 Views: 1072 | Post Order: 7

Write for us