- VBScript 教程
- VBScript - 首頁
- VBScript - 概述
- VBScript - 語法
- VBScript - 啟用
- VBScript - 佈局
- VBScript - 變數
- VBScript - 常量
- VBScript - 運算子
- VBScript - 決策
- VBScript - 迴圈
- VBScript - 事件
- VBScript - Cookies
- VBScript - 數字
- VBScript - 字串
- VBScript - 陣列
- VBScript - 日期
- VBScript 高階
- VBScript - 過程
- VBScript - 對話方塊
- VBScript - 面向物件
- VBScript - 正則表示式
- VBScript - 錯誤處理
- VBScript - 其他語句
- VBScript 有用資源
- VBScript - 問答
- VBScript - 快速指南
- VBScript - 有用資源
- VBScript - 討論
VBScript - 佈局
在 HTML 檔案中放置 VBScript
在 HTML 文件中的任何位置包含 VBScript 程式碼都具有靈活性。但是,在 HTML 檔案中包含 VBScript 的最優方式如下:
在 <head>...</head> 部分中使用指令碼。
在 <body>...</body> 部分中使用指令碼。
在 <body>...</body> 和 <head>...</head> 部分中使用指令碼。
在外部檔案中使用指令碼,然後將其包含在 <head>...</head> 部分中。
在以下部分中,我們將瞭解如何以不同的方式放置 VBScript:
在 <head>...</head> 部分中使用 VBScript
如果希望在某些事件發生時執行指令碼,例如使用者單擊某個位置時,則可以按如下方式將該指令碼放置在 head 中:
<html>
<head>
<script type = "text/Vbscript">
<!--
Function sayHello()
Msgbox("Hello World")
End Function
//-->
</script>
</head>
<body>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</body>
</html>
這將產生以下結果:一個名為 SayHello 的按鈕。單擊按鈕後,將向用戶顯示一個訊息框,其中包含訊息“Hello World”。
在 <body>...</body> 部分中使用 VBScript
如果需要在頁面載入時執行指令碼,以便該指令碼在頁面中生成內容,則該指令碼位於文件的 <body> 部分。在這種情況下,您將不會使用 VBScript 定義任何函式:
<html>
<head> </head>
<body>
<script type = "text/vbscript">
<!--
document.write("Hello World")
//-->
</script>
<p>This is web page body </p>
</body>
</html>
這將產生以下結果:
Hello World This is web page body
在 <body> 和 <head> 部分中使用 VBScript
您可以將 VBScript 程式碼同時放在 <head> 和 <body> 部分中,如下所示:
<html>
<head>
<script type = "text/vbscript">
<!--
Function sayHello()
msgbox("Hello World")
End Function
//-->
</script>
</head>
<body>
<script type = "text/vbscript">
<!--
document.write("Hello World")
//-->
</script>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</body>
</html>
這將產生以下結果:顯示“Hello World”訊息和一個“Say Hello”按鈕。單擊按鈕後,將向用戶顯示一個包含訊息“Hello World”的訊息框。
Hello World
在外部檔案中使用 VBScript
當您開始更廣泛地使用 VBScript 時,您可能會發現有些情況下,您會在站點的多個頁面上重複使用相同的 VBScript 程式碼。您無需在多個 HTML 檔案中維護相同的程式碼。
script 標籤提供了一種機制,允許您將 VBScript 儲存在外部檔案中,然後將其包含到 HTML 檔案中。以下是一個示例,說明如何使用 script 標籤及其 src 屬性在 HTML 程式碼中包含外部 VBScript 檔案:
<html>
<head>
<script type = "text/vbscript" src = "filename.vbs" ></script>
</head>
<body>
.......
</body>
</html>
要使用來自外部檔案源的 VBScript,您需要將所有 VBScript 原始碼寫入副檔名為“.vbs”的簡單文字檔案中,然後如上所示包含該檔案。例如,您可以將以下內容儲存在 filename.vbs 檔案中,然後在包含 filename.vbs 檔案後,您可以在 HTML 檔案中使用 sayHello 函式。
Function sayHello() Msgbox "Hello World" End Function
在 QTP 中放置 VBScript
VBScript 放置在 QTP(Quick Test Professional)工具中,但它**不**包含在 HTML 標籤內。指令碼檔案以 .vbs 副檔名儲存,並由 Quick Test Professional 執行引擎執行。