简体   繁体   English

Javascript作用域问题还是实例化问题?

[英]Javascript Scoping Issue or Instantiation Issue?

I have a webpage that does some calculations on the fly based on user 'keyup' events in a text input field. 我有一个网页,它根据文本输入字段中的用户“键”事件即时进行一些计算。 It's a rather large page that is built dynamically, so I have to keep a Hash/Array of values that I can reference from other locations within the page. 这是一个动态构建的相当大的页面,因此我必须保留一个哈希值/数组,我可以从页面中的其他位置引用这些值。

So at the top of the page I am doing this - 因此,在页面顶部,我正在这样做-

var itemBalanceHash = new Array();
var itemID_2_POIDHash = new Array();
var currentPODocumentID;
var warningArray = new Array();

Then I go on to define a number of functions that use these variables I just defined. 然后,我继续定义一些使用刚刚定义的这些变量的函数。 However, it is not until after I finish defining these functions that these variables start getting used (ie filled with stuff.) 但是,直到我定义完这些函数之后,这些变量才开始被使用(即充满了东西)。

<script type="text/javascript">                                
itemBalanceHash["${topitem.itemIdentifier}"]="${topitem.itemOutstandingEncumberedAmount}";
itemID_2_POIDHash["${topitem.itemIdentifier}"] = currentPODocumentID;
</script>

This script block appears in the middle of my page as it is getting built. 该脚本块在构建时会出现在页面的中间。 In fact, it gets called multiple times depending on how many rows I need to fill (a result set of a query from the database). 实际上,根据需要填充多少行(数据库查询的结果集),它会被多次调用。

Anyhow - This works great in Firefox. 无论如何-在Firefox中效果很好。 However, it does not work in IE8, Chrome or Safari. 但是,它不适用于IE8,Chrome或Safari。 I haven't tested this in older versions of IE; 我没有在较旧版本的IE中进行过测试; but I assume it will not work there either. 但我认为它也不行。

The error message Internet Explorer Debugger is giving me is this Internet Explorer调试器给我的错误消息是这个

"itemBalanceHash is undefined" “ itemBalanceHash未定义”

So, my question: Is this a scoping issue? 所以,我的问题是:这是一个范围界定问题吗?
Firefox seems to have corrected my error somewhere, but where? Firefox似乎在某处纠正了我的错误,但是在哪里?

I can provide more code if necessary. 如果需要,我可以提供更多代码。 Thanks for your help! 谢谢你的帮助!
It is most appreciated! 非常感谢!

So the issue was with iframes! 因此问题出在iframe上! Firefox executes all the JS code in my JSP page for me. Firefox会为我执行JSP页面中的所有JS代码。 IE, Chrome and Safari needed to have Event Listeners attached to the page that would trigger the load event that would then execute my JS code (where the instantiation for this object was supposed to happen). IE,Chrome和Safari需要在页面上附加事件侦听器,该事件侦听器将触发加载事件,然后该事件将执行我的JS代码(应该在该对象的实例化的地方发生)。

Very interesting. 很有意思。 I expected something like this from Microsoft, but I'm surprised Google and Safari behave in a similar fashion. 我原本希望微软提供类似的服务,但是我很惊讶Google和Safari的行为类似。 Just another reason why FF is my default browser. FF是我的默认浏览器的另一个原因。

Thanks for everyone's help/responses. 感谢大家的帮助/回应。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM