1.head标记中的脚本
1 <html>
2 <head>
3 <meta charset="UTF-8">
4 <title>head中定义的JS函数</title>
5 <script type="text/javascript">
6 funtion message()
7 {
8 alert("调用JS函数!sum(100,200)="+sum(100,200));
9 funtion sum(x,y)
10 {
11 return x+y; //返回函数计算结果
12 }
13 }
14 </script>
15 </head>
16 <body>
17 <h4>head标记内定义两个JS函数</h4>
18 <form>
19 <input name="btnCallJS" type="button" onclick="message();" value="计算并显示两个数的和">
20 </form>
21 </body>
22 </html>
2.body标记中的脚本
1 <html>
2 <head>
3 <meta charset="UTF-8">
4 <title>第一个JavaScript实例</title>
5 </head>
6 <body>
7 <script type="text/javascript">
8 document.write("第一个JavaScript实例!");
9 </script>
10 </body>
11 </html>
3.外部js文件中的脚本
1 <!--demo.js-->
2 funtion message()
3 {
4 alert("调用外部js文件的函数");
5 }
1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 <title>调用外部js文件的JaveScript函数</title> 5 <script type="text/javascript" src="dmeo.js"></script> 6 </head> 7 <body> 8 <form> 9 <input name="btnCallJS" type="button" onclick="message();" value="调用外部js文件的JavaScript函数"> 10 </form> 11 </body> 12 </html>
4.事件处理的代码中的脚本
1 <html>
2 <head>
3 <meta charset="UTF-8">
4 <title>直接在事件处理代码中加入JavaScript代码</title>
5 </head>
6 <body>
7 <form>
8 <input type="button" onclick="alert('直接在事件处理代码中加入JavaScript代码')" value="直接调用JavaScript代码">
9 </form>
10 </body>
11 </html>
来源:https://www.cnblogs.com/gly1120/p/9090342.html