javascript脚本应该写在代码的什么地方?

1.HEAD中的脚本,是可以保证脚本在任何调用之前被加载。
2.BODY中的脚本,当页面被加载时执行的脚本放在HTML的body部分。放在body部分的脚本通常被用来生成页面的内容。
3.Javascript可以放在页面的任何地方,它的加载顺序与页面的加载顺序一致,页面加载时,先加载head部分,后加载body部分,所以当把javascript代码块放在HTML前面时,它将先于head被加载,当放在head里面时,它将先于body被加载,当放在页面最后的的html之外时,它将最后被加载。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script >
function CloseWin()
{
window.opener=null;
window.open('','_self');
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="关闭" onclick ="CloseWin()" />
</div>
</form>
</body>
</html>

====================或者放置body内,或者head,body之间也有效果=======================

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<script >
function CloseWin() 

window.opener=null; 
window.open('','_self'); 
window.close(); 

</script>
<input id="Button1" type="button" value="button" onclick ="CloseWin()" />
</div>
</form>
</body>
</html>

原文地址:https://www.cnblogs.com/proving/p/11358285.html