Create an interface for data sharing in js-Simplify mutual value transfer between frameworks

Source: Internet
Author: User
Tags delete cache

Many frameworks have parent-child relationships, which are very troublesome to operate. Many people often see such sad code:
Copy codeThe Code is as follows:
Extends extends parent.doc ument. getElementById ("main ")
.ContentWindow.doc ument. getElementById ('input'). value =
Document. getElementById ('myiframe ')
.Content1_doc ument. getElementById ('s0'). value;

In fact, this problem can be greatly simplified. There is a fixed window in the framework application called window. top: If we cache data to this page, all the frameworks under it can be obtained, no matter how the sub-page changes. No Cookie or HTML5 local database policy is required. You only need to reference a js file on each page. The content is as follows:
Copy codeThe Code is as follows:
Var share = {
/**
* Cross-framework data sharing interface
* @ Param {String} refers to the data name stored
* @ Param {Any} Any data to be stored (the queried data is returned if this item is not included)
*/
Data: function (name, value ){
Var top = window. top,
Cache = top ['_ cache'] || {};
Top ['_ cache'] = CACHE;
Return value? Cache [name] = value: cache [name];
},
/**
* Data Sharing and deletion Interface
* @ Param {String} The Name Of The Deleted Data
*/
RemoveData: function (name ){
Var cache = window. top ['_ cache'];
If (cache & cache [name]) delete cache [name];
}
};

The method with a few lines can share any type of data for each frame page to read. It has nothing to do with the page name and level, even if the Framework page level is modified someday, you don't have to worry about it. It works properly.
For example, we can store shared data on page:
Copy codeThe Code is as follows:
Share. data ('myblog', 'HTTP: // www.jb51.net ');
Share. data ('edittitle', function (val ){
Document. title = val;
});

Then, the data of page A is retrieved from any frame page.
Copy codeThe Code is as follows:
Alert ('My blog address is: '+ share. data ('myblog ');
Var editTitle = share. data ('edittitle ');
EditTitle ('I have obtained the data ');

Yes, that's easy! You can also see this technology in the iframeTools extension of artDialog.

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.