一、head元素中
HeadScript.aspx
代码解释:当页面执行到元素中,出发load事件后调用message()
%@ Page Language="C#" AutoEventWireup="true" CodeFile="HeadJS.aspx.cs" Inherits="Chap2_HeadJS" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
function message() {
alert("在\<head\>元素中");/*提示框*/
}
</script>
</head>
<body onload="message()">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
二、在body元素中
BodyJS.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BodyJS.aspx.cs" Inherits="Chap2_BodyJS" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script>
document.write("在<body>元素中");
</script>
</div>
</form>
</body>
</html>
三、在独立的.js文件中
在网站中右键 添加 文件夹Scripts
右击Scripts 添加 添加新项 选择JavaScript文件 文件名FileJS.js
添加一下代码
function message() {
alert("在\<head\>元素中");/*提示框*/
}
(引用js直接用鼠标拖到元素中)
(Chap文件夹)新建web窗体 FileJS.aspx
%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileJS.aspx.cs" Inherits="Chap2_FileJS" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../Scripts/FileJS.js"></script>
</head>
<body onload="message()">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>