JavaScript腳本通常放置在三個位置:
1、head部分JavaScript腳本。
2、body部分JavaScript腳本。
3、單獨以.js結尾的文件中的JavaScript腳本。
客戶端會順序讀取並解析文檔內容,body部分的JavaScript腳本會優先執行,其他部分的JavaScript腳本會通過調用執行。
head部分JavaScript腳本
<html> <head> <script type="text/javascript"> widow.onload=function() { alert("該提示框是通過 onload 事件調用的。") } </script> </head> <body> </body> <html>
body部分JavaScript腳本
<html> <head> </head> <body> <script type="text/javascript"> document.write("該消息在頁面加載時輸出。") </script> </body> </html>
單獨以.js結尾的文件中的JavaScript腳本
<html> <head> </head> <body> <script src="/js/example_externaljs.js"> </script> <p> 實際的腳本位於名為 "xxx.js" 的外部腳本中。 </p> </body> </html>