Different between JavaScript into <head></head> and into <body></body>

JavaScript between <head></head> Tag :

JavaScript between<head></head> tag generally writing as a function & function call from HTML content of the webpage, like this :

Here I have made a JavaScript function “displaydate()” between <head></head> tag and calling this function inside html content. According to function it will display date into “<p id=”demo”></p>”



<html>
<head>
<script type=”text/javascript”>

function displaydate(){

document.getElementById(“demo”).innerHTML=Date();

}

</script>
</head>
<body>

<h1>My First Web Page</h1>

<p id=”demo”></p>
<script type=”text/javascript”>displaydate();</script>
</body>

</html>


Now JavaScript between<body></body> Tag :

JavaScript between <body></body> tag generally writing as a function or inline code, like this :

Here I just wrote a inline JavaScript code to display date” between <body></body> tag. There not need to call any function, when page request date will show on page. According to function it will display code into “<p id=”demo”></p>”
<html>

<head></head>

<body>

<h1>My First Web Page</h1>
<p id=”demo”></p>
<script type=”text/javascript”>document.getElementById(“demo”).innerHTML=Date();</script>
</body>

</html>

  • Share/Save/Bookmark

Leave a Reply

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