Describe the two ways to embed a JavaScript script in an HTML document.
时间: 2023-11-22 17:56:35 浏览: 107
在页面中引入js的两种方法(推荐)
There are two ways to embed a JavaScript script in an HTML document:
1. Inline Script: In this method, we can embed the JavaScript code inside the HTML document in the `<script>` tag. For example:
```
<!DOCTYPE html>
<html>
<head>
<title>Inline Script Example</title>
</head>
<body>
<h1>JavaScript Inline Script Example</h1>
<script>
alert("Hello World!");
</script>
</body>
</html>
```
2. External Script: In this method, we can create a separate JavaScript file with the `.js` extension and include it in the HTML document using the `<script>` tag. For example:
```
<!DOCTYPE html>
<html>
<head>
<title>External Script Example</title>
<script src="myscript.js"></script>
</head>
<body>
<h1>JavaScript External Script Example</h1>
</body>
</html>
```
Where `myscript.js` is a file containing the JavaScript code.
阅读全文