iframe Tag and Message Mechanism
Add an iframe tag to the project and add a message mechanism to enable communication between the parent page and the iframe page.
Add an iframe tag
Add an iframe tag to the project page and apply styles to it.
<iframe id="sview_iframe" style="width:100%;height:100%" src="./sview/sview3d.html">
Add a message mechanism
Add an iframe message receiving method and a method for sending messages to the iframe in the JS file:
window.addEventListener('message', function(event) {
// Message received from the iframe
console.log('Received message from iframe:', event.data);
}, false);
// Send a message to the iframe. Note that the message must be sent after the iframe has finished loading; otherwise, the iframe will not receive it.
document.getElementById("sview_iframe").contentWindow.postMessage("Hello from parent!", "*");
Add message handling logic to receive messages sent from the parent page and determine the path of the model to open. The following code is for reference only; the specific approach can be adjusted as needed by modifying the js/main.js and js/main_local.js files.
window.onload = function () {
var path;
window.addEventListener('message', function(event) {
// Can receive a model path message; set path to the received path, then open the model
console.log('Received message from parent:', event.data);
path = event.data.path;
var type = event.data.type;
var name = event.data.name;
let fileObjs = parseFileInfo([[path, name, type]]);
if (!fileObjs || fileObjs.length === 0) {
return; // No file information
}
// Native H5 rendering
initOnline(basePath, fileObjs);
}, false);
}