📄 js_whereto.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 Where To</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 Where To ...</h1>
<a href="js_howto.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_statements.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">JavaScripts in the body section will be executed WHILE the page loads.</p>
<p class="intro">JavaScripts in the head section will be executed when CALLED.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_headsection">Head section</a><br />
Scripts that contain functions go in the head section of the document. Then we
can be sure that the script is loaded before the function is called.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_bodysection">Body section</a><br />
Execute a script that is placed in the body section.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_externalexample">External script</a><br />
How to access an external script.</p>
<hr />
<h2>Where to Put the JavaScript</h2>
<p>JavaScripts in a page will be executed immediately while the page loads into the
browser. This is not always what we want. Sometimes we want to execute a script
when a page loads, other times when a user triggers an event.</p>
<p><b>Scripts in the head section:</b> Scripts to be executed when they are
called, or when an event is triggered, go in the head section. When you place a
script in the head section, you will ensure that the script is loaded before
anyone uses it. </p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre><html>
<head>
<span class="insert"><script type="text/javascript">
....
</script></span>
</head></pre>
</td></tr></table>
<p><b>Scripts in the body section:</b> Scripts to be executed when the page
loads go in the body section. When you place a script in the body section it
generates the content of the page.</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre><html>
<head>
</head>
<body>
<span class="insert"><script type="text/javascript">
....
</script></span>
</body></pre>
</td></tr></table>
<p><b>Scripts in both the body and the head section:</b> You can place an
unlimited number of scripts in your document, so you can have scripts in both
the body and the head section.</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre><html>
<head>
<span class="insert"><script type="text/javascript">
....
</script></span>
</head>
<body>
<span class="insert"><script type="text/javascript">
....
</script></span>
</body></pre>
</td></tr></table>
<br />
<hr />
<h2>Using an External JavaScript</h2>
<p>Sometimes you might want to run the same JavaScript on several pages, without
having to write the same script on every page.</p>
<p>To simplify this, you can write a JavaScript in an external file. Save the
external JavaScript file with a .js file extension.</p>
<p><b>Note:</b> The external script cannot contain the <script> tag!</p>
<p>To use the external script, point to the .js file in the "src" attribute
of the <script> tag:</p>
<table class="ex" cellspacing="0" border="1" width="100%"><tr><td>
<pre><html>
<head>
<span class="insert"><script src="xxx.js"></script></span>
</head>
<body>
</body>
</html></pre>
</td></tr></table>
<p><b>Note:</b> Remember to place the script exactly where you normally would
write the script!</p>
<hr />
<a href="js_howto.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_statements.asp"><img border="0" alt="next" 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 + -