Today, in the project encountered a problem, two JS page to share a JS object. JS global variables and static variables are not good, other hard to force the small farmers do not have to insist. And LZ do not want to use cookies to store, one is unsafe, two people like. Finally found a super method to solve this problem, that is, using window.top[' _cache ' to store this variable, you can achieve, different JSP page directly object sharing.
var share = {
/**
* cross-frame data sharing interface
* @param {String} stored data name
* @param {any} data that will be stored (no such return is queried)
* *
data:function (name, value) {
var top = window.top,
cache = top[' _cache '] | | {};
top[' _cache '] = CACHE;
return value!== undefined? Cache[name] = Value:cache[name];
/**
* Data sharing Delete Interface
* @param {String} deleted data name
* * Removedata:function (name) {
var cache = window.top[' _cache '];
if (cache && cache[name]) Delete cache[name];
}
;
The following LZ code is attached:
LZ a JSP page for a.jsp, click a button in a.jsp to open another b.jsp page. LZ's ideas are as follows:
In the a.jsp open b.jsp event, write the following code:
window.top[' _cache '] = chatfrdlist;
window.top[' _cache '][frduserid] = Frduserid;
Where chatfrdlist is defined as var chatfrdlist = new Object ();
Frduserid is the ID of a user.
Then, in an event in b.jsp, you can do the following:
<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 you close the Chat window with that buddy, remove it from the chat table </pre>
Then, the LZ can be in a.jsp other events to operate window.top[' _cache ', so that you can achieve multiple JSP page Direct JS object sharing.