Home  >  Article  >  Web Front-end  >  Why do we use JavaScript in HTML?

Why do we use JavaScript in HTML?

王林
王林forward
2023-08-29 10:33:021213browse

为什么我们在 HTML 中使用 JavaScript?

First, we will understand what JavaScript is. When a website does more than just sit there and provide you with static information, such as showing timely content updates, interactive maps, animated 2D/3D visuals, scrolling video jukeboxes, etc., JavaScript is almost certainly being used. Additionally, you can add advanced functionality to your website using scripting or the programming language JavaScript. The first two layers of the cake made up of standard web technologies are HTML and CSS. This floor is the third floor.

Levels of web pages

  • The markup language we use to structure and define the meaning of web page content is called HTML. Using HTML, we can define paragraphs, headings, and data tables, and embed images and videos into web pages.

  • CSS stands for Cascading Style Sheets and is a style rule language that we use to apply styles to HTML content, such as establishing background colors and fonts and organizing them into columns.

  • JavaScript is a scripting language that lets you do everything from building dynamically updating materials to managing multimedia and animated graphics. Amazing things can be done with just a few lines of JavaScript code.

The use of JavaScript in web development

JavaScript is a computer language used on the web. JavaScript can be used to update and modify HTML and CSS. It can be used to calculate, change and validate data. Users can interact with web pages using JavaScript. There's really no limit to what you can accomplish with JavaScript; here are just a few examples of using it on web pages -

  • You can expose or hide more information by pressing the button.

  • The color of the button changes when the mouse is hovering over it.

  • The website has a carousel of images that you can scroll through.

  • Use the timer or countdown on the website to zoom in or out of the photo,

  • Play audio and video content within the page,

  • Use hamburger dropdown menus and more for animation and display.

The purpose of JavaScript on web pages

When you load a web page in your browser (browser tab), you are executing code (HTML, CSS, and JavaScript) in the execution environment. This is similar to a factory that inputs raw materials (code) and produces finished products (web pages).

Through the Document Object Model API, JavaScript is often used to edit HTML and CSS to dynamically update the user experience. Remember that a web document's code typically loads and runs in the order it appears on the page. If JavaScript loads and runs before HTML and CSS, which means modifications are being made, errors may occur.

How to add JavaScript to the page?

Similar to how CSS is applied to HTML pages, the same is true for JavaScript. However, JavaScript requires an HTML tag, <script>. </script>

In the following example, we will see how users can embed JavaScript code into the head tag using the script element in an HTML file.

Example

As shown in the example below, we embed JavaScript in the HTML file to modify the HTML. Inside the script tag, we use a function called "ParaChange". We create a "Click Here" button inside the body tag. The ParaChange function will be called by the button's click event. This function uses the DOM's document.getElementById() to manipulate the p tag and change the statement.

<html>
<head>
   <script>
  
      // inside the head tag is a script tag.
      // The onclick event calls the parachange function
      function ParaChange() {
         document.getElementById("para").innerHTML = "The line has changed.";
      }
   </script>
</head>
<body>
   <h2>Including JavaScript in HTML document in <i>Head tag</i></h2>
   <p id="para">The is a line</p>
   <button type="button" onclick="ParaChange()">Click Here</button>
</body>
</html>

Users may notice that the paragraph line within the p tag of the click event has changed due to clicking the button. This is because the "ParaChange" function written within the script tag of the head tag is called during that click event.

In this way, we can use JavaScript in HTML to create our flexible and dynamic pages by writing complex code.

The above is the detailed content of Why do we use JavaScript in 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