Javascript学习与实践(3)
Js用在那里?
Js用在<head>和<body>中是有区别的,JS在BODY块中,页面一加载就会去解析执行它,JS在HEAD中需要调用才能执行,所以一般建议在要调用的HEAD中
例子:文档中在HEAD页面包含的功能脚本,我们能确保他,早调用之前就被加载到页面中
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event")
}
</script>
</head>
<body onload="message()">
</body>
</html>
body块中,执行放在里面的脚本,
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("This message is written when the page loads")
</script>
</body>
</html>
外部的脚本----怎么链接一个外部文件脚本
<html>
<head>
</head>
<body>
<script src="xxx.js">
</script>
<p>
The actual script is in an external script file called "xxx.js".
</p>
</body>
</html>