Super Method for sharing one js object among multiple jsp pages

Source: Internet
Author: User

Today, there is a problem in the project that two js pages need to share one. js object. Neither js global variables nor static variables can be forced by other hard-pressed small farmers. LZ does not want to store cookies. One is insecure and the other is preferred. Finally, we found a super method to solve this problem. That is, we can use window. top ['_ cache'] to store this variable and achieve direct object sharing between different Jsp pages.

Var share = {/*** cross-framework data sharing interface * @ param {String} Name of the data stored * @ param {Any} Any data to be stored (If this option is not specified, the returned data is queried) data) */data: function (name, value) {var top = window. top, cache = top ['_ cache'] | |{}; top [' _ cache'] = CACHE; return value! = Undefined? Cache [name] = value: cache [name] ;},/***** data name deleted by the data sharing and deletion interface * @ param {String} */removeData: function (name) {var cache = window. top ['_ cache']; if (CACHE & cache [name]) delete cache [name] ;}};


The following is the LZ code:

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;

The chatFrdList is defined as var chatFrdList = new Object ();

FrdUserId is the id of a user.

In an event of B. jsp, you can perform the following operations:

<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 closing the chat window with this friend, remove it from the chat table </pre>


Then, LZ can operate window. top ['_ cache'] in other events of A. jsp, so that js objects can be directly shared across multiple JSP pages.

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.