A Function Can Be Set Up to Accept a Variable Number of Arguments
<html>
<head>
<title>A Function Can Be Set Up to Accept a Variable Number of Arguments</title>
<script type="text/javascript">
<!--
function welcomeMessage(userName) {
if (userName != null) {
document.writeln("Hello, " + userName);
}else{
document.writeln("Welcome to our Web site!");
}
numArgs = welcomeMessage.arguments.length;
if(numArgs > 1) {
for(var i = 1; i < numArgs; i++) {
document.writeln("\""+welcomeMessage.arguments[i]+"\"");
}
}
}
// -->
</script>
</head>
<body>
<script type="text/javascript">
<!--
var userName = "David", extraMsg = "It has been a long time!";
var userName2 = null;
var extraMsg1 = "Would you like to become a member?";
var extraMsg2 = "You can enroll online!";
welcomeMessage(userName, extraMsg);
document.writeln("<hr>"); welcomeMessage(userName2, extraMsg1, extraMsg2);
// -->
</script>
</body>
</html>
Related examples in the same category