包含javascript文件可以访问父文档中的全局变量吗? [英] do included javascript files have access to global variables in the parent document?

查看:64
本文介绍了包含javascript文件可以访问父文档中的全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下这样的代码:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
  <HTML>
  <HEAD>
  <TITLE>BLAH</TITLE>
  <script language='Javascript' type='text/javascript'>
  var ScriptVersionReqd='1.0';
  </script>

  <script language='JavaScript' type='text/javascript' src='clientscript.js'></script>
  etc. etc.

clientscript.js是否可以访问变量ScriptVersionReqd?如果没有,为什么不呢?

Does clientscript.js have access to the variable "ScriptVersionReqd"? If not, why not?

推荐答案

是的。

只要全局变量在被外部脚本调用之前已被置于全局范围内。

As long as the global variable has been put into global scope before it is called by the external script.

编辑以回复评论:有关javascript变量范围的详细说明,请参阅此处

Edit in response to a comment: See here for a good explanation of javascript variable scope.

这篇关于包含javascript文件可以访问父文档中的全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆