views:

254

answers:

3

Hi, 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. 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. Specifically, the document does action on $(document).ready, and those actions dictate what must be done on later function calls.

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

Internal 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?

+1  A: 

If you want to know about scoping read this. It might help you to work out what is going on.

Mark Dickinson
Love Crockford.
Alan
A: 

Have you tried declaring saveMe outside of the $(document).ready? Then you should be able to change the value from inside the $(document).ready as well as from the external script. 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.

DLH
A: 

Making the variable a member of the function object worked swimmingly.

Stefan Kendall