A super method for multiple JSP pages to share a JS object


Today, I encountered a problem in the project. Two JS pages want to share a JS object. JS global variables and static variables are not good, other forced small farmers do not want to force. And LZ doesn’t want to store it with cookies. First, it’s not safe. Two people like it. Finally, we found a super way to solve this problem, which is to use window.top [] Cache ‘] to store this variable, which can realize direct object sharing among different JSP pages.

var share = { 

*Cross framework data sharing interface 
*The data name stored in @ param {string} 
*Any data to be stored by @ param {any} (without this item, the queried data will be returned) 
data: function (name, value) { 
var top = window.top, 
cache = top['_CACHE'] || {}; 
top['_CACHE'] = cache; 

return value !== undefined ? cache[name] = value : cache[name]; 

*Data sharing deletion interface 
*The data name deleted by @ param {string} 
removeData: function (name) { 
var cache = window.top['_CACHE']; 
if (cache && cache[name]) delete cache[name]; 


The code of LZ is attached below:

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;

Where chatfrdlist is defined as var chatfrdlist = new object();

Frduserind is the ID of a user.

Then, in an event of b.jsp, you can perform the following operations:

<pre name="code" style="margin-top:0px; margin-bottom:0px; padding:0px; font-family:'courier new',courier,monospace">var e = document.getElementsByName("chatWindow");</pre><pre name="code" 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 > and

LZ can then operate in other events of a.jsp window.top [] In this way, multiple JSP pages can be directly shared by JS objects.

Recommended Today

Basic knowledge of react

React.FunctionComponent ReactProvides a component typeReact.FunctionComponent, can be abbreviatedReact.FC, Can receive a genericp, the default is{} children, return aReact.ReactNodeThis onechildrenIt’s anythingcomponentAll of them Static propertiesdefaultProps, the default property of the component, which can not be passed externally. interface IHelloProps { message?: string; } const Hello: React.FunctionComponent<IHelloProps> = (props) => { return <h2>{props.message}</h2>; }; Hello.defaultProps = { […]