- 15天學(xué)會(huì)JavaScript(視頻教學(xué)版)
- 王金柱
- 1060字
- 2019-12-06 14:00:30
1.2 網(wǎng)頁中的JavaScript腳本語言
本節(jié)介紹HTML網(wǎng)頁中的JavaScript腳本語言,具體就是如何在網(wǎng)頁中使用JavaScript腳本語言的方法。
1.2.1 <script>標(biāo)簽
如果想在HTML網(wǎng)頁中使用JavaScript腳本語言,那么就一定要用到<script>標(biāo)簽,無論是嵌入式腳本還是外部腳本。當(dāng)然,<script>既然是一種標(biāo)簽元素,就需要有開始和結(jié)束標(biāo)記,具體見下面的代碼:
<script> // TODO // 腳本代碼 … </script>
這里需要說明的一點(diǎn)是,上面的寫法適用于最新的HTML5版本網(wǎng)頁的,而對于早期的網(wǎng)頁(如HTML 4.01版本)則必須使用以下的寫法:
<script type="text/javascript"> … </script>
必須要在<script>標(biāo)簽中加入“type="text/javascript"”的描述,這樣寫才能讓瀏覽器正確識別HTML網(wǎng)頁。
1.2.2 嵌入式JavaScript腳本
在網(wǎng)頁中使用JavaScript腳本語言比較直接的方法就是將JavaScript腳本嵌入到網(wǎng)頁代碼中。設(shè)計(jì)人員可以在HTML網(wǎng)頁中的任何位置使用<script>標(biāo)簽定義嵌入式JavaScript腳本代碼。
下面,介紹如何在HTML網(wǎng)頁中使用嵌入式JavaScript腳本代碼(詳見源代碼ch01目錄中ch01-js-embedding.html文件)。
【代碼1-1】
01 <!doctype html> 02 <html lang="en"> 03 <head> 04 <!-- 添加文檔頭部內(nèi)容 --> 05 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 06 <meta http-equiv="Content-Language" content="zh-cn" /> 07 <link rel="stylesheet" type="text/css" href="css/style.css"> 08 <script> 09 alert("JavaScript 基礎(chǔ) - 嵌入js腳本代碼"); 10 </script> 11 <title>JavaScript in 15-days</title> 12 </head> 13 <body> 14 <!-- 添加文檔主體內(nèi)容 --> 15 </body> 16 </html>
關(guān)于【代碼1-1】的分析如下:
第08~10行代碼通過<script>標(biāo)簽定義一段嵌入式JavaScript腳本語言,第09行的JS代碼通過“alert()”函數(shù)定義一個(gè)彈出式警告提示框。
下面通過FireFox瀏覽器測試一下【代碼1-1】所定義的HTML頁面,查看一下頁面中嵌入式JavaScript腳本代碼的執(zhí)行效果,如圖1.2所示。

圖1.2 嵌入式JavaScript腳本代碼
由圖1.2可知【代碼1-1】中第09行JS代碼定義的彈出式警告提示框在頁面加載過程中成功顯示出來了,注意到此時(shí)頁面應(yīng)該還沒有加載完成,關(guān)于這一點(diǎn)我們在后文中會(huì)給出原因分析。
1.2.3 引入外部JavaScript腳本文件
在HTML網(wǎng)頁中引入外部JavaScript腳本文件是另一種使用JavaScript腳本語言的方法。這種方法非常適用于需要使用大量JavaScript腳本代碼的情況,尤其是在應(yīng)用JavaScript框架的場景,一般稱該方法為外鏈?zhǔn)剑↙inking)JavaScript腳本。
外鏈?zhǔn)剑↙inking)JavaScript腳本的基本使用方法如下:
<script src="xxx.js"></script>
“src”屬性用于定義外部JavaScript文件的路徑地址。其中,路徑可以為絕對路徑或相對路徑,這要看具體項(xiàng)目的情況。
下面就將【代碼1-1】稍加修改,按照外鏈?zhǔn)絁avaScript腳本進(jìn)行設(shè)計(jì),具體代碼如下(詳見源代碼ch01目錄中ch01-js-linking.html文件)。
【代碼1-2】
01 <!doctype html> 02 <html lang="en"> 03 <head> 04 <!-- 添加文檔頭部內(nèi)容 --> 05 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 06 <meta http-equiv="Content-Language" content="zh-cn" /> 07 <link rel="stylesheet" type="text/css" href="css/style.css"> 08 <script type="text/javascript" src="js/ch01-js-linking.js"></script> 09 <title>JavaScript in 15-days</title> 10 </head> 11 <body> 12 <!-- 添加文檔主體內(nèi)容 --> 13 </body> 14 </html>
關(guān)于【代碼1-2】的分析如下:
第08行代碼通過<script>標(biāo)簽定義了外鏈?zhǔn)絁avaScript腳本,其中“src”屬性定義外部腳本的相對路徑地址("js/ch01-js-linking.js")。
關(guān)于上面引入的外部腳本文件的具體代碼如下(詳見源代碼ch01目錄中js/ch01-js-linking.js文件)。
【代碼1-3】
01 alert("JavaScript 基礎(chǔ) - 外鏈?zhǔn)絡(luò)s腳本");
關(guān)于【代碼1-3】的分析如下:
第01行JS代碼通過“alert()”函數(shù)定義了一個(gè)彈出式警告提示框,這與【代碼1-1】中第09行代碼的功能類似。
下面運(yùn)行測試【代碼1-2】使用外鏈?zhǔn)絁avaScript腳本定義的HTML頁面,頁面打開后的效果如圖1.3所示。

圖1.3 外鏈?zhǔn)絁avaScript腳本
如圖1.3所示,外鏈?zhǔn)絁avaScript腳本與嵌入式JavaScript腳本實(shí)現(xiàn)的功能是完全相同的,只不過是定義手法不同而已。
- Java Web開發(fā)學(xué)習(xí)手冊
- Mastering Visual Studio 2017
- Learn Type:Driven Development
- DevOps for Networking
- 21天學(xué)通C++(第6版)
- Learning Three.js:The JavaScript 3D Library for WebGL
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- Java圖像處理:基于OpenCV與JVM
- Troubleshooting Citrix XenApp?
- Mastering Apache Storm
- Web Developer's Reference Guide
- Mastering Embedded Linux Programming
- 3D Printing Designs:The Sun Puzzle
- Puppet 5 Beginner's Guide(Third Edition)
- Getting Started with Web Components