Transmitting data between an iframe and a parent window or a child window is a troublesome matter. It would be very convenient if we could write a once-and-for-all interface. Here is a brief introduction on how to implement this function. The principle is to cache data as early as the window.top, so that no matter how the level of the child window and parent window changes, the data will always exist and will not change.
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]; } } }; share.data('mayi','http://www.jb51.net');
The above code meets our requirements. The code is relatively simple. You can analyze it yourself. If you have any questions, you can leave a message.
Do you know how to transfer values between Iframes in JS? Let me give you a brief introduction below.
1. Get the elements of the parent page in the iframe sub-page:
a>window.parent.document is to get the object in 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 in the iframe subpage in the parent page:
a>
var child = document.getElementByIdx_x("mainFrame").contentWindow;//mainFrame id is the id of the parent page iframe
child.document; //Get the document object in the child page;