JavaScript Tutorial/Function/Function Call

Материал из Web эксперт
Перейти к: навигация, поиск

Call a function in hyperlink

<html>
<head>
<title>Calling a Function</title>
<script language="javascript" type="text/javascript">
<!--
function greetVisitor()
{
     var myName = prompt("Name?", "");
     alert("Welcome " + myName + "!")
}
//-->
</script>
</head>
<body>
<h1>Please click below and enter your name when prompted.</h1>
<P><a href="javascript:greetVisitor()">Click for a greeting</a></p>
</body>
</html>


Call function inside a function

<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function get_added_text(textpart1,textpart2)
{
 var added_text=textpart1+" "+textpart2;
 return added_text;
}
function write_text()
{
 var thetext=get_added_text("Hi","there!");
 document.write(thetext);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
<!--
write_text();
//-->
</SCRIPT>
</BODY>
</HTML>


Call your function

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
yourMessage();
function yourMessage()
{
    alert("Your first function!");
}
//  -->
</script>
</head>
<body>
</body>
</html>


Call your function in body onLoad event

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function yourMessage()
{
    alert("Your first function!");
}
//  -->
</script>
</head>
<body onLoad="yourMessage()">
</body>
</html>


Function call sequence

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function firstMessage()
{
    alert("Here is the first message!");
    secondMessage();
    alert("And here is the third!");
}
function secondMessage()
{
    alert("And here is the second!");
}
//  -->
</script>
</head>
<body onLoad="firstMessage()">
</body>
</html>


Nested function call

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function firstMessage()
{
    alert("Here is the first message!");
    secondMessage();
}
function secondMessage()
{
    alert("And here is the second!");
}
//  -->
</script>
</head>
<body onLoad="firstMessage()">
</body>
</html>