普通函数的调用
<html>
<head>
<script language="javascript">
//尝试把函数定义放到调用代码的后面
function myFunction(){
return("Hello, have a nice day!");
}
</script>
</head>
<body>
<script language="javascript">
document.write(myFunction());
</script>
<p>The script in the body section calls a function.</p>
<p>The function returns a text.</p>
</body>
</html>
在链接中调用函数 用户点击链接后,即调用该函数
<html>
<head>
<title>Functions</title>
<script language=javascript>
function greetings(){ // Function defined within <head> tags
document.bgColor="lightblue";
alert("Greetings to you!");
}
</script>
</head>
<body><center>
<a href="javascript:greetings()"><big>Click here for
Salutations</big>
</a><br>
</center>
</body>
</html>
由事件触发调用函数
<html>
<head<title>Functions and Events</title>
<script language=javascript>
function greetings(){ // Function definition
document.bgColor="pink";
alert("Greetings and Salutations! ");
}
</script>
</head>
<body><center>
<form>
<input type="button" value="Welcome button" onClick="greetings();">
</form>
</body>
</html>