一:内部脚本
把JS代码定义在HTML页面中
1.JavaScript代码必须位于<script></script>标签之间
2.在HTML文档中,可以在任意地方,放置任意数量的<script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>引入方式</title>
<script>
alert("Hello JS");
</script>
</head>
<body>
<script>
alert("Hello JS");
</script>
</body>
</html>
<script>
alert("Hello JS");
</script>
3.一般会把脚本置于<body>元素的底部,目的是为了改善网页的显示速度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>引入方式</title>
</head>
<body>
</body>
</html>
<script>
alert("Hello JS");
</script>
二:外部脚本
把JS代码定义在外部JS文件中,然后引入到HTML页面中
1.定义到外部JS文件中时,只包含JS代码,不需要将代码置于<script</script>标签之间
2.<script>标签不能自闭合
demo .js
alert("Hello JS");
引入方式.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>引入方式</title>
<script src="JS/demo.js"></script>
</head>
<body>
</body>
</html>
<script>
alert("Hello JS");
</script>