Today, there is a problem in the project that two js pages need to share one. js object. Neither js global variables nor static variables can be forced by other hard-pressed small farmers. LZ does not want to store cookies. One is insecure and the other is preferred. Finally, we found a super method to solve this problem. That is, we can use window. top ['_ cache'] to store this variable and achieve direct object sharing between different Jsp pages.
Var share = {/*** cross-framework data sharing interface * @ param {String} Name of the data stored * @ param {Any} Any data to be stored (If this option is not specified, the returned data is queried) data) */data: function (name, value) {var top = window. top, cache = top ['_ cache'] | |{}; top [' _ cache'] = CACHE; return value! = Undefined? Cache [name] = value: cache [name] ;},/***** data name deleted by the data sharing and deletion interface * @ param {String} */removeData: function (name) {var cache = window. top ['_ cache']; if (CACHE & cache [name]) delete cache [name] ;}};
The following is the LZ code:
A jsp page of LZ is A. jsp. Click A button in A. jsp to open another B. jsp page. The idea of LZ is as follows:
In the event that A. jsp opens B. jsp, write the following code:
window.top['_CACHE'] = chatFrdList; window.top['_CACHE'][frdUserId] = frdUserId;
The chatFrdList is defined as var chatFrdList = new Object ();
FrdUserId is the id of a user.
In an event of B. jsp, you can perform the following operations:
<Pre name = "code" class = "javascript" style = "margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: 'courier new ', courier, monospace "> var e = document. getElementsByName ("chatWindow"); </pre> <pre name = "code" class = "javascript" style = "margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: 'courier new', courier, monospace "> var keyId = e [0]. id; delete window. top ['_ cache'] [keyId]; // when closing the chat window with this friend, remove it from the chat table </pre>
Then, LZ can operate window. top ['_ cache'] in other events of A. jsp, so that js objects can be directly shared across multiple JSP pages.