Home  >  Article  >  Web Front-end  >  How does inline JavaScript work with HTML?

How does inline JavaScript work with HTML?

PHPz
PHPzforward
2023-09-01 14:33:06484browse

How does inline JavaScript work with HTML?

In this article, you will learn how inline JavaScript works with HTML. Inline JavaScript represents a block of code written between <script> tags in an html file. The advantage of using inline JavaScript in HTML files is that it reduces round trips from the web browser to the server. </script>

Example 1

Let’s learn how to add basic statements in html files using inline JavaScript -

<!DOCTYPE html >
<html>
<head>
   <h2> This is a simple HTML file</h2>
   <script>
      document.write("Hi, This is an inline Javascript code written in between script tags");
   </script>
</head>
</html>

illustrate

  • Step 1 - In a simple html file, add the title tag

    and display the title of your choice.

  • Step 2 - Add the script tag <script> below the title tag. </script>

  • Step 3 -In the script tag, use the document.write() function to print the text.

Example 2

In this example, let us use inline JavaScript to print an array from an html file.

<!DOCTYPE html >
<html>
<head>
   <h2> This is a simple HTML file</h2>
   <script>
      document.write("An Inline Javascript code to display an array")
      document.write("<br>")
      const inputArray = [3,4,5,6,7,8]
      document.write("The array is defined as :", inputArray)
   </script>
</head>
</html>

illustrate

  • Step 1 - In a simple html file, add the title tag

    and display the title of your choice.

  • Step 2 - Add the script tag <script> below the title tag. </script>

  • Step 3 - In the script tag, define an array, inputArray, and add values ​​to the array.

  • Step 4 - Print the array using document.write() function.

The above is the detailed content of How does inline JavaScript work with HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more