简体   繁体   English

JavaScript变量范围 - 持久状态?

[英]JavaScript variable scoping - persisting state?

I have the following setup, and I need to know how to persist state. 我有以下设置,我需要知道如何保持状态。

1.) An external web page uses ajax to load and display a .jsp file, which contains javascript. 1.)外部网页使用ajax加载并显示包含javascript的.jsp文件。 2.) Once this rendering is complete, javascript in the .jsp file must be called to perform an action based on the results that occurred during rendering. 2.)完成此渲染后,必须调用.jsp文件中的javascript,以根据渲染过程中发生的结果执行操作。 Specifically, the document does action on $(document).ready, and those actions dictate what must be done on later function calls. 具体来说,文档对$(document).ready执行操作,这些操作决定了后续函数调用必须执行的操作。

The question I have is this: How do I persist the data and state created when the page is loaded? 我的问题是:如何在加载页面时保留创建的数据和状态? I tried something like this: 我试过这样的事情:

External: ajax to test.jsp 外部:ajax到test.jsp

Internal test.jsp 内部test.jsp

var saveMe = {};

function getsCalled()  
{  
    saveMe = {'a':function(){return false;}};  
}  

function needsData()  
{  
    //???  
}  

Later... 后来...
External: 外部:

needsData();  

Nothing I seem to attempt is working. 我似乎没有尝试任何工作。 What would be the best way to persist state in this situation? 在这种情况下坚持国家的最佳方式是什么?

If you want to know about scoping read this . 如果你想了解范围,请阅读此内容 It might help you to work out what is going on. 它可能会帮助您弄清楚正在发生的事情。

Have you tried declaring saveMe outside of the $(document).ready? 你有没有尝试在$(文件).ready之外声明saveMe? Then you should be able to change the value from inside the $(document).ready as well as from the external script. 然后你应该能够从$(document).ready以及外部脚本中更改值。 I'm not sure how the scoping works for javascript variables from an ajax call though, so I'm not sure if this would actually work. 我不确定如何从ajax调用中获取javascript变量的作用域,所以我不确定这是否真的有效。

使变量成为函数对象的一个​​成员。

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

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