JavaScript Where To

The JavaScript where to explain which tags are required to put JavaScript code. There are many methods for JavaScript where to code. These JavaScript where to methods are described below:

The <script> Tag

Using the <script> and </script> tags in HTML, JavaScript code is inserted.

<!DOCTYPE html>
<html>
<body>

<script>
document.write("Welcome code");
</script>

</body>
</html>

Output:

JavaScript Where To

The <head> tag

When we used JavaScript function in our code, it is written in the <head> section of an HTML page. When a button is clicked a function is called:

<!DOCTYPE html>
<html>
<head>
<script>
function Fun() {
  document.getElementById("tacode").innerHTML = "It is a function.";
}
</script>
</head>
<body>

<p id="tacode">Function demo</p>
<button type="button" onclick="Fun">Check the function</button>
</body>
</html>

Output:

JavaScript Where To

The <body> tag

We can also place a JavaScript function in the section of an HTML page. We can use it for function calls:

<!DOCTYPE html>
<html>
<body>

<p id="tacode">Function demo</p>

<button type="button" onclick="Fun">Check the function</button>

<script>
function Fun() {
  document.getElementById("tacode").innerHTML = "It is a function.";
}
</script>

</body>
</html>

Output:

image 58

External JavaScript

We can write the scripts in external files:

The code written in external files can be accessed many times. We saved the external file with the .js extension. When we need these files we access them using the below code:

External References

We can write external files in the following ways:

  • With a URL
  • With a file path
  • Without any path