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

How does inline JavaScript work with HTML?

PHPz
Release: 2023-09-01 14:33:06
forward
531 people have browsed it

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>
Copy after login

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. </p></li> <li><p><b>Step 3</b> -In the script tag, use the document.write() function to print the text. </p></li> </ul> <h2>Example 2</h2> <p>In this example, let us use inline JavaScript to print an array from an html file. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>&lt;!DOCTYPE html &gt; &lt;html&gt; &lt;head&gt; &lt;h2&gt; This is a simple HTML file&lt;/h2&gt; &lt;script&gt; document.write(&quot;An Inline Javascript code to display an array&quot;) document.write(&quot;&lt;br&gt;&quot;) const inputArray = [3,4,5,6,7,8] document.write(&quot;The array is defined as :&quot;, inputArray) &lt;/script&gt; &lt;/head&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div> <h2>illustrate</h2> <ul class="list"> <li> <p><b>Step 1</b> - In a simple html file, add the title tag </p> <h2> and display the title of your choice. </h2> </li> <li><p><b>Step 2</b> - 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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template