Fix irame message handling

This commit is contained in:
Michael Wyraz 2021-12-02 21:07:25 +01:00
parent e9917737de
commit 1c93d924de
3 changed files with 10 additions and 15 deletions

View File

@ -1,6 +1,6 @@
<html>
<iframe src="./index.html" style="border: 1px solid red; width: 95%; height: 95%" id="meshviewer-embedded"></iframe>
<script type="text/javascript" src="./embed.js" />
<script type="text/javascript" src="./embed.js"></script>
</html>

View File

@ -11,22 +11,17 @@
}
function updateIframeHash() { // see https://gist.github.com/manufitoussi/7529fa882ff0b737f257
if(iframe.contentWindow.location.host !== "") {
// iframe already loaded.
iframe.contentWindow.location.hash = window.location.hash;
} else {
// iframe is just starting.
var newHash = window.location.hash;
var srcStr = iframe.getAttribute('src');
var words = srcStr.split('#');
var href = words[0];
var newSrc = href + newHash;
iframe.setAttribute('src', newSrc);
}
// iframe is just starting.
var newHash = window.location.hash;
var srcStr = iframe.getAttribute('src');
var words = srcStr.split('#');
var href = words[0];
var newSrc = href + newHash;
iframe.setAttribute('src', newSrc);
};
updateIframeHash();
iframe.contentWindow.addEventListener("message", (event) => {
window.addEventListener("message", (event) => {
if (event && event.data && event.data.hash) {
window.location.replace(event.data.hash);
}

View File

@ -72,7 +72,7 @@ define(['moment', 'utils/router', 'leaflet', 'gui', 'helper', 'utils/language'],
}
window.onhashchange = function () {
window.postMessage({hash: window.location.hash}, '*');
parent.postMessage({hash: window.location.hash}, '*');
};
var language = new Language();