Skip to content

Commit b0460fc

Browse files
committed
Add article content
1 parent 226e04b commit b0460fc

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
# JavaScript
2-
learn, JavaScript
1+
# JavaScript-Example(JavaScript学习记录)
2+
3+
> Learn JavaScript Again
4+
5+
记录自己曾经遗漏的小知识,系统性的学习JavaScript体系。过程中避免不了要涉及HTML,以及CSS的内容。
6+
此系列文章可能更适合我自己的知识体系,如果其中恰好也有帮助到你的知识点,那真是甚感荣幸。
7+
8+
## 知识点列表:
9+
10+
1. [][在HTML中使用JavaScript](./article/001-use-javascript-in-html.md)
11+
2. [-]基本概念-数据类型
12+
3. [-]基本概念-函数
13+
14+
待完成...
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 在 HTML 中使用 JavaScript
2+
3+
### 1、`<script>`标签的位置
4+
5+
按照惯例`<script>`标签应该放在页面的`<head>`元素中,例如:
6+
7+
```html
8+
<html>
9+
<head>
10+
<title>Example HTML page</title>
11+
<!--通常写法-->
12+
<script type="application/javascript" src="test.js"></script>
13+
</head>
14+
<body>
15+
<!-- 这里放内容 -->
16+
</body>
17+
</html>
18+
```
19+
20+
目的:
21+
22+
- 将需要外部引入的文件(css、js)在相同位置统一引入;
23+
24+
缺点:
25+
26+
- 页面必须等到js、css下载、解析、执行完成才开始渲染页面,出现白屏;
27+
28+
**解决方案:**
29+
30+
- **`<script>`引用放在`<body>`最后。如下:**
31+
32+
```html
33+
<html>
34+
<head>
35+
<title>Example HTML page</title>
36+
</head>
37+
<body>
38+
<!-- 这里放内容 -->
39+
<script type="application/javascript" src="test.js"></script>
40+
</body>
41+
</html>
42+
```
43+
44+
### 2、script 标签属性
45+
46+
`<script>`标签在HTML4.01中定义了6个属性:
47+
48+
#### 必须属性:
49+
- type: 指示脚本的 MIME 类型
50+
51+
#### 可选属性:
52+
- defer: 规定是否对脚本执行进行延迟,直到页面加载为止
53+
- async: 规定异步执行脚本(仅适用于外部脚本)。
54+
- charset: 规定在外部脚本文件中使用的字符编码。
55+
- language: 已废弃,请使用 type 属性代替它。
56+
- **src: 规定外部脚本文件的 URL(跨域运用了这个属性可访问其他域的特性)**
57+
58+
#### 结论
59+
最稳妥的办法还是把`<script>`写在`<body>`底部,**没有兼容性问题,没有白屏问题,没有执行顺序问题**,高枕无忧,慎用defer和async,这要是为什么这两个属性不流行的原因。

testFile/test.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Example HTML page</title>
4+
</head>
5+
<body>
6+
<!-- 这里放内容 -->
7+
<script type="application/javascript" src="test.js"></script>
8+
</body>
9+
</html>

testFile/test.js

Whitespace-only changes.

testFile/test.json

Whitespace-only changes.

0 commit comments

Comments
 (0)