生成文本
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
生成普通文本和标签
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>
head 部分
<html>
<head>
<script type="text/javascript">
function message()
{
alert("该提示框是通过 onload 事件调用的。")
}
</script>
</head>
<body onload="message()">
</body>
</html>
body 部分
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("该消息在页面加载时输出。")
</script>
</body>
</html>
外部 JavaScript
<html>
<head>
</head>
<body>
<script src="../js/example_externaljs.js" tppabs="http://www.w3school.com.cn/js/example_externaljs.js">
</script>
<p>
实际的脚本位于名为 "xxx.js" 的外部脚本中。
</p>
</body>
</html>