Home >Web Front-end >HTML Tutorial >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>
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>
Step 1 - In a simple html file, add the title tag
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.
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>
Step 1 - In a simple html file, add the title tag
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!