函式
建立函式
宣告函式
function realcool()
宣告函式內的程式碼
function reallycool()
{
JavaScript程式碼
}
函式命名
函式的參數
function reallycool(coolcar, coolplace)
{
JavaScript程式碼
}
函式參數的值
function reallycool(coolcar, coolplace)
{
var mycar = coolcar;
}
function reallycool(coolcar, coolplace)
{
document.write("My car is a " + coolcar);
}
使用多個參數
function reallycool(coolcar, coolplace)
{
document.write("My car is a " + coolcar + " and I drive it to " + coolplace);
}
函式內的 return 敘述
function hello(name)
{
return "Hello, Mr. or Ms. " + name;
}
在 script 中呼叫函式
hello("Lee");
在 HEAD 區域呼叫函式
建立 JavaScript 訊息方塊
window.alert("This is an alert!");
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
window.alert("This is an alert!");
//-->
</SCRIPT>
</HEAD>
<BODY>
HTML 碼
</BODY>
</HTML>
</PRE>
在函式中呼叫 JavaScript 訊息方塊
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function show_message()
{
window.alert("This is an alert!");
}
show_message();
//-->
</SCRIPT>
</HEAD>
<BODY>
HTML 碼
</BODY>
</HTML>
</PRE>
在 BODY 區域呼叫函式
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function print_text()
{
document.write("I came from a function!");
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H4>在 BODY 區域呼叫函式</H4>
<SCRIPT language="JavaScript">
<!--
print_text();
//-->
</SCRIPT>
<p>
<B>This is a bold statement!</B>
</BODY>
</HTML>
從一個函式呼叫另一個函式
<HEAD>
<SCRIPT language="JavaScript">
<!--
function update_alert()
{
window.alert("Welcome! This site is updated daily!");
}
function call_alert()
{
update_alert();
}
call_alert();
//-->
</SCRIPT>
</HEAD>
另一例子:
<HEAD>
<SCRIPT language="JavaScript">
<!--
function update_alert()
{
window.alert("Welcome! This site is updated daily!");
}
function section_alert()
{
window.alert("Please visit the picture section!");
}
function links_alert()
{
window.alert("Also, check out my links page!");
}
function get_message()
{
update_alert();
section_alert();
links_alert();
}
get_message();
//-->
</SCRIPT>
</HEAD>
呼叫有參數的函式
<HEAD>
<SCRIPT language="JavaScript">
<!--
function alerts( message )
{
window.alert( message );
}
function get_message()
{
alerts("Welcome! This site is updated daily!");
alerts("Please visit the picture section!");
alerts("Also, check out my links page!");
}
get_message();
//-->
</SCRIPT>
</HEAD>
使用全域變數
<SCRIPT language="JavaScript">
<!--
var mycar = "Honda";
var paycheck = 1200;
function new_car()
{
mycar = "Ferrari"; // 全域變數
paycheck = 3500; // 全域變數
window.alert( "You need $" + paycheck + " to get a " + mycar );
}
new_car();
window.alert( "You make $" + paycheck + " and have a " + mycar );
//-->
</SCRIPT>
使用區域變數
<SCRIPT language="JavaScript">
<!--
var mycar = "Honda";
var paycheck = 1200;
function new_car()
{
var mycar = "Ferrari"; // 區域變數
var paycheck = 3500; // 區域變數
window.alert( "You need $" + paycheck + " to get a " + mycar );
}
new_car();
window.alert( "You make $" + paycheck + " and have a " + mycar );
//-->
</SCRIPT>
呼叫有 return 敘述的函式
<SCRIPT language="JavaScript">
<!--
function get_added_text()
{
var text1 = "This is ";
var text2 = "fun!";
var added_text = text1 + text2;
return added_text;
}
var alert_text = get_added_text();
window.alert( alert_text );
//-->
</SCRIPT>
來源出處:
https://www.csie.ntu.edu.tw/~sylee/courses/jscript/function.htm