从理论上来讲,在HTML文档中任何地方加加入javascript代码都是可行的,只是顺序不同,执行时间不同
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>执行代码</title>
<script type="text/javascript">
alert('first script block');
alert('first script block-Second Line');
</script>
</head>
<body>
<h1>Test Page</h1>
<script type="text/javascript">
alert('Second Script Block');
</script>
<p>Some more HTML</p>
<script type="text/javascript">
alert('Third script Block');
function doSomething()
{
alert('Function in Third script Block');
//函数内部的代码只有函数被调用的时候才会执行;就像国家法律一样,只要你不犯法法律就不会被执行,在这里一样,这要不调用,函数内部的代码不会被执行
}
doSomething(); //函数被调用
</script>
</body>
</html>