Where to put JavaScript tag in html | CodingSource

Where to put JavaScript tag in html

Javascript files are added to the website in two ways. The first is inline, the second is to prepare a separate javascript file and add it externally.

WRITE BETWEEN <head> LABELS in JavaScript

In the example below, a JavaScript function is placed in the <head> section of an HTML page.

To add JavaScript in the HTML page, use the <script> tag.

The <Script> and </script> tags specify where the JavaScript code starts and ends.

<html>

<head>

<title>Hello World!</title>

   <script>

   // JavaScript codes are written here.

   </script>

</head>

<body>

   <p>JavaScript Trial page</p>

</body>

<html>

NOTE: In old examples, <Script type = "text / javascript"> type attribute has been removed in HTML5. You can write directly as <Script> and </script>.

If we prepare it as a separate javascript file and save it with the .js extension and add it to the html document, this goes into adding an external javascript file.

EXAMPLE: save as test.js.

     alert("Hello World!");

To add to HTML:

<script type="text/javascript" src="https://www.codingsource.net/test.js"></script>

We get the same result with both methods we used above. There are advantages to adding it with an external javascript file.

  • There is no code complexity in writing it in a separate file from the HTML page. Updates are easy in one file.
  • Pages load fast Pages that we update the cached javaScript file by pulling information from the database load fast.

JAVASCRIPT ON <head> and <body> Tags

You can place unlimited JavaScript codes inside HTML pages.

JavaScript commands can be in the <head> and <body> sections, or both sections.

The usual practice is to put pieces of JavaScript code in the application section, or at the bottom of the page. If you can place it below the HTML tags, it will not interfere with the content of the page.

You have seen the placement of JavaScript codes between the <head> tags above. Now in the example below, a JavaScript function is placed in the <body> section of an HTML page. Clicking a button executes the JavaScript function.

<!DOCTYPE html>

<html>

  <body>

<h1>JavaScript test page</h1>

<p id="test">Trial text</p>

<button type="button" onclick="Try()">Change</button>

<script>

function Test() {

    document.getElementById("test").innerHTML = "JavaScript test page";

}

</script>

  </body>

</html>

JAVASCRIPT DESCRIPTION LINE

Comment lines, as the name suggests, are used to explain JavaScript codes, and they help us later when we do some editing on the code. Comment lines are ignored by browsers. A JavaScript comment line takes two forms; It is displayed as single-line and multi-line.

// This is a comment line

/* Description first line

Description second line

Description third line */

Comment lines are seen as comments by the browser and are not processed. The comment line is a method that allows the programmer to take notes between the codes that go on and on. The Website Code page that we will do while coding may consist of a few lines as well as tens of pages. When we take a look at the website code page we have written after a certain time, it can sometimes cause a waste of time to understand where we are and what we are doing. That's why comment lines can be used.

Script Codes Related Posts

Newer Post Load More Pages..