The goal is to dynamically control the loading of content in the bottomFrame from centerFrame , and in the centerFrame from indexLeftPanel . Since the two embedded instances cannot do anything directly with each other, we will write a “message dispatcher” in the main instance.html instance. Those. when the main instance is loaded, the “register” will be loaded (in the figure, arrow No. 1) in the embedded one and they will be able to exchange messages. Thus, with the implemented documents, it is possible to manage other documents (arrows №2, 3).
To begin, load center.html in centerFrame , to do this, click “Change HTML in center frame”.
Everything is normal here, changing centerFrame.src happens in the usual way from mainlayout.js , loaded into index.html , because it happens in one instance - index.html :
listeners: { click: function () { var mainFrame = document.getElementById("centerFrame"); mainFrame.src = 'layout/center.html'; } }
<script type="text/javascript"> if (window.addEventListener) { window.addEventListener("message", listener, false); } else { window.attachEvent("onmessage", listener); } function listener(event) { var sO = event.data; if (sO) { if (sO.action == acNavigate) { var iframe = document.getElementById(sO.frame); if (iframe) iframe.src = sO.source; } } } </script>
Cooking center.html :
<iframe id="centerFrame" src="" width="100%" height="100%" frameborder="0" onload="loadPage_centerFrame()"></iframe> function loadPage_centerFrame() { var centerFrame = document.getElementById("centerFrame"); if (centerFrame) { var sO = sendObject; sO.action = acInit; centerFrame.contentWindow.postMessage(sO, '*'); } }
if (window.addEventListener) { window.addEventListener("message", listener, false); } else { window.attachEvent("onmessage", listener); }
var mainWindow = null; function listener(event) { mainWindow = event.source; }
All is ready.
Yes, the sendObject record is passed to the loadPage_centerFrame () function , in fact, this record is not used in the prototype, unlike the real docks, in which this record is used to transfer service information.
Now we ’ll load bottom.html into bottomFrame by clicking on the “Change HTML under bottom frame” link from center.html . When you click on the link, the function from crossdocmess.js is called :
function linkclick(frame, link) { if (mainWindow) { var sO = sendObject; sO.action = acNavigate; sO.frame = frame; sO.source = link; mainWindow.postMessage(sO, "*"); } }
Source: https://habr.com/ru/post/340272/
All Articles