Multiple JSP pages share a JS object

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, 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.

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.