A super method of sharing a JS object with multiple JSP pages

Source: Internet
Author: User
Tags delete cache

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; Margi
n-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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.