JavaScript Tutorial/Language Basics/Source File
Содержание
- 1 Define javascript code in BODY section
- 2 Define message in one block and output it in another
- 3 Define script in HEAD section
- 4 HTML Template Page supporting the JavaScript
- 5 Mix complicated code with HTML
- 6 Mix simple code with HTML
- 7 Reference javascript source file stored outside
- 8 Use the "script" tag to incluse your javascript code
Define javascript code in BODY section
<html>
<head>
</head>
<body>
<h1>Page heading</h1>
<script>
alert("Do you see the page heading?");
</script>
</body>
</html>
Define message in one block and output it in another
<html>
<head>
<title>Define message in one block and output it in another</title>
<script language="javascript" type="text/javascript">
<!--
var msg = "Message in javascript";
//-->
</script>
</head>
<body>
<h1>Define message in one block and output it in another</h1>
<script language="javascript" type="text/javascript">
<!--
document.write("<P>" + msg + "</p>");
//-->
</script>
</body>
</html>
Define script in HEAD section
<html>
<head>
<script>
alert("Do you see the page heading?");
</script>
</head>
<body>
<h1>Page heading</h1>
</body>
</html>
HTML Template Page supporting the JavaScript
<html>
<head>
<title>HTML Template Page</title>
<script language="javascript">
<!--
// -->
</script>
</head>
<body>
</body>
</html>
Mix complicated code with HTML
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
// -->
</script>
</head>
<body>
<p onclick="units = prompt("How many do you want","Enter quantity");alert("Cost per unit is $9.99, therefore total cost for " + units + " is " + units * 9.99);">Click here to specify quantity</p>
</body>
</html>
Mix simple code with HTML
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
// -->
</script>
</head>
<body>
<p onclick="prompt("How many do you want","Enter quantity");">Click here to specify quantity</p>
</body>
</html>
Reference javascript source file stored outside
<html>
<head>
<title>Title of Document</title>
<script src="path-to-file/fileName.js"></script>
</head>
<body>
The content of
your page goes here.
</body>
</html>
Use the "script" tag to incluse your javascript code
<html>
<head>
<title>Title of Document</title>
<script>
// All Your Javascript Code goes In Here Between the opening and closing script tags.
</script>
</head>
<body>
The content of
your page here.
</body>
</html>