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>
<html>
<head>
    <title>Title</title>
    <script>
        function AlertMe() {
            alert("I am called from JavaScript!");
        }
    </script>
</head>
<body>
    <input type="button" id="btnTest" onclick="AlertMe()" value="Click me!" />
</body>
</html>

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

 Views: 5434 | Post Order: 7



Write for us






Hosting Recommendations