⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 js_functions.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Functions</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

<h1>JavaScript Functions</h1>
<a href="js_popup.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_loop_for.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">A function is a reusable code-block that will be executed by an event, or
when the function is called.</p>
<hr />

<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_function1">Function</a><br />
How to call a function.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_function2">Function with arguments</a><br />
How to pass a variable to a function, and use the variable in the function.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_functionarg2">Function with arguments 2</a><br />
How to pass variables to a function, and use these variables in the function.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_function_return2">Function that returns a value</a><br />How to let the function return a value.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_function_return">A function with arguments, that returns a value</a><br />How to let the function find the 
product of two arguments and return the result.</p>
<hr />

<h2>JavaScript Functions</h2>
<p>To keep the browser from executing a script when the page loads, 
you can put your script into a function.</p>
<p>A function contains code that will be executed by an event or by a 
call to that function.</p>
<p>You
may call a function from anywhere within the page (or even from other pages if 
the function is embedded in an external .js file).</p>
<p>Functions can be defined both in the &lt;head&gt; and in the &lt;body&gt; section of a 
document. However, to assure that the function is read/loaded by the browser 
before it is called, it could be wise to put it in the &lt;head&gt; section.</p>
<h3>Example</h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1"><tr><td>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function displaymessage()
{
alert(&quot;Hello World!&quot;);
}
&lt;/script&gt;
&lt;/head&gt;</pre>
<pre>&lt;body&gt;
&lt;form&gt;
&lt;input type=&quot;button&quot; value=&quot;Click me!&quot;
onclick=&quot;displaymessage()&quot; &gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</td></tr></table>
<br />
If the line: alert(&quot;Hello world!!&quot;) in the example above had not been 
put 
within a function, it would have been executed as soon as the line was loaded. 
Now, the script is not executed before the user hits the button. We have added 
an onClick event to the button that will execute the function displaymessage() 
when the button is clicked.<p>You will learn more about JavaScript events in the 
JS Events chapter.</p>
<hr />

<h2>How to Define a Function</h2>
<p>The syntax for creating a function is:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table2"><tr><td>
<pre>function <i>functionname</i>(<i>var1,var2,...,varX</i>)
{
<i>some code</i>
}</pre>
</td></tr></table>
<p>var1, var2, etc are variables or values passed into the function. The { and 
the } defines the start and end of the function.</p>
<p><b>Note:</b> A function with no parameters must include the parentheses () 
after the function name:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3"><tr><td>
<pre>function <i>functionname</i>()
{
<i>some code</i>
}</pre>
</td></tr></table>
<p><b>Note:</b> Do not forget about the importance of capitals in JavaScript! 
The word function must be written in lowercase letters, otherwise a JavaScript 
error occurs! Also note that you must call a function with the exact same 
capitals as in the function name.<br />
</p>
<hr />

<h2>The return Statement</h2>
<p>The return statement is used to specify the value that is returned from the 
function.</p>
<p>So, functions that are going to return a value must use the return
statement.</p>
<h3>Example</h3>
<p>The function below should return the product of two numbers (a and b):</p>
<table class="ex" cellspacing="0" border="1" width="100%"><tr><td>
<pre>function prod(a,b)
{
x=a*b;
return x;
}</pre>
</td></tr></table>
<p>When you call the function above, you must pass along two parameters:</p>
<table class="ex" cellspacing="0" border="1" width="100%"><tr><td>
<pre>product=prod(2,3);</pre>
</td></tr></table>
<p>The returned value from the prod() function is 6, and it will be stored in the variable
called product.</p>
<hr />

<h2>The Lifetime of JavaScript Variables</h2>
<p>When you declare a variable within a function, the variable can only be
accessed within that function. When you exit the function, the
variable is destroyed. These variables are called local variables. You can have
local variables with the same name in different functions, because each is
recognized only by the function in which it is declared.</p>
<p>If you declare a variable outside a function, all the functions on your page can
access it. The lifetime of these variables starts when they are declared, and
ends when the page is closed.</p>
<hr />

<a href="js_popup.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_loop_for.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -