JavaScript IFRAME Data sharing interface implementation method _javascript skills

Source: Internet
Author: User

It's a hassle to pass data to the parent window or to the child window, and if we can write a permanent interface that's convenient, here's a brief introduction to how to implement this feature. The principle is to window.top the data cache as early as possible, so that the data will always remain unchanged regardless of how the parent window of the child window changes.

The code is as follows:

var share={ 
data:function (name,value) { 
var top=window.top, 
cache=top[' _cache ']| | {}; 
top[' _cache ']=cache; 
return value?cache[name]=value:cache[name]; 
}, 
removedata:function (name) { 
var cache=window.top[' _ CACHE ']; 
if (Cache&&cache[name]) 
{ 
delete cache[name];}} 
; 

The above code to achieve our requirements, the code is relatively simple, we can analyze their own, if you have any questions can post message.

Do you know how to pass the value of the IFRAME in JS? Below to give you a brief introduction.

1. Get the elements of the parent page in the IFRAME child page:

A>window.parent.document This is the object that gets the parent page document;
B> If you want to get the method in the parent page JS: window.parent.xxxx (); xxxx () is the method;

2. Get the elements from the IFRAME child page in the parent page:
A>
var child = document.getelementbyidx_x ("MainFrame"). Contentwindow;//mainframe this ID is the ID of the parent page iframe
child.document;//gets the document object in the child page;

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.