Which is the best practise to write code in javascript. Here i added java script in head tag(1st example) and body tag(2nd example) with example. If both are correct then what is the use of these two different method. Please throw some light on it.
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Sample for Java-Script</h1>
<p id="para1">This is p tag</p>
<div id="div1">This is div</div>
<p>
<button type="button" onclick="func()">Click To Run</button>
</p>
<script>
function func() {
document.getElementById("para1").innerHTML = "Hello There.";
document.getElementById("div1").innerHTML = "How's Life?";
}
</script>
<p>When you click on "click to run", the two elements will change.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function func() {
document.getElementById("para1").innerHTML = "Hello There.";
document.getElementById("div1").innerHTML = "How's Life?";
}
</script>
</head>
<body>
<h1>Sample for Java-Script</h1>
<p id="para1">This is p tag</p>
<div id="div1">This is div</div>
<p>
<button type="button" onclick="func()">Click To Run</button>
</p>
<p>When you click on "click to run", the two elements will change.</p>
</body>
</html>