定义在中的变量,为什么可以在外使用
定义在<script></script>中的变量,为什么可以在<script></script>外使用,请详细讲解一下,变量在此类情况下的使用范围。代码如下:
请注意代码中的变量 n 为啥可以使用在 <script></script> 之外的body区域中
<html>
<head>
<meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title> 动态页面 </title>
</head>
<body>
<script type="text/javascript">
// 计数器
var n = 0;
var win = null;
// 用于显示弹出窗口显示提示信息的函数
var show = function(msg)
{
// 判断弹出窗口是否为空
if ((win == null) || (win.closed))
{
// 打开一个新的弹出窗口
win = window.open("","console"
,"width=340,height=220,resizable");
// 将弹出窗口的文档打开成一个text/html文档
win.document.open("text/html");
}
// 让弹出窗口得到焦点
win.focus();
// 在弹出窗口装载的文档中输出信息
win.document.writeln(msg);
}
</script>
<!-- 激发事件的按钮 -->
<input type="button" value="单击"
onclick="show('您单击了按钮:' + ++n + '次。<br/>');">
</body>
</html>