JavaScript腳本在頁面中放置的位置


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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 關於