Today, in the project encountered a problem, two JS page to share a JS object. JS global variables and static variables are not, other hard-pressed smallholder farmers do not have to insist. And the LZ do not want to use cookies to store, one is not safe, two people likes. Finally found a super method to solve this problem, that is, with window.top[' _cache '] to store this variable, can be implemented, different JSP page direct object sharing.
var share = {/** * cross-frame data sharing interface * @param {String} stored data name * @param {any} will store any data (without this item will return the queried data) */data:function (name, value) {V AR top = Window.top,cache = top[' _cache ' | | {};top[' _cache '] = Cache;return value!== undefined? Cache[name] = value:cache[name];},/** * Data Share Delete interface * @param {String} deleted data name */removedata:function (name) {var cache = Windo w.top[' _cache '];if (CACHE && cache[name]) delete cache[name];}};
The following is the LZ code:
LZ a JSP page for a.jsp, click on a button in a.jsp to open another b.jsp page. LZ's idea 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;
where chatfrdlist is defined as var chatfrdlist = new Object ();
Frduserid is the ID of a user.
So, in an event in b.jsp, you can do the following:
var e = document.getelementsbyname ("Chatwindow");
var keyId = e[0].id;delete window.top[' _cache '][keyid];//when you close a chat window with that friend, remove it from the chat table
The LZ can then operate window.top[' _cache ' in other events in a.jsp, so that it can implement multiple JSP page direct JS object sharing.