search
HomeWeb Front-endFront-end Q&AHow to add javascript to html

How to add javascript to html

May 27, 2023 am 09:02 AM

HTML and JavaScript are the two most commonly used client-side technologies. HTML is the foundation of web pages, while JavaScript enhances the interactivity and dynamic nature of websites. When writing HTML code, we can easily add JavaScript code to achieve more functions and effects. This article explains how to add JavaScript to HTML.

I. Using built-in tags

HTML 5 introduced the built-in <script></script> tag for embedding JavaScript code in pages. You can place the <script></script> tag at the beginning or end of your HTML code and add JavaScript code within it. For example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Demo</title>
  <meta charset="UTF-8">
</head>
<body>
  <h1 id="欢迎来到-JavaScript-世界">欢迎来到 JavaScript 世界</h1>
  <script>
    alert("Hello World!");
  </script>
</body>
</html>

In the above code, we added a simple alert dialog box in the <script></script> tag to use when the page loads Display message. It is important to note that if you put the <script></script> tag in the head, you should use an async or lazy attribute to load the JavaScript file to ensure that the HTML page is not blocked by JavaScript code.

II. Using external files

If your JavaScript code is long or needs to be used in multiple pages, it is best to save it in a separate .js file and use external <script></script> tag for reference. For example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Demo</title>
  <meta charset="UTF-8">
  <script src="script.js"></script>
</head>
<body>
  <h1 id="欢迎来到-JavaScript-世界">欢迎来到 JavaScript 世界</h1>
</body>
</html>

In the above code, we referenced a file named script.js## using the src attribute of the

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor