Passed
Pull Request — develop (#317)
by
unknown
01:21
created

embed.js ➔ updateIframeHash   A

Complexity

Conditions 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.3333
c 0
b 0
f 0
cc 5
1
(function() {
2
3
    var iframe=document.getElementById("meshviewer-embedded")
4
    if (!iframe) {
5
        console.log("IFrame 'meshviewer-embedded' not found")
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
6
        return;
7
    }
8
    if (!iframe.contentWindow) {
9
        console.log("Element 'meshviewer-embedded' seems not to be a valid iframe")
10
        return;
11
    }
12
13
    function updateIframeHash() { // see https://gist.github.com/manufitoussi/7529fa882ff0b737f257
14
        if(iframe.contentWindow.location.host !== "") {
15
            // iframe already loaded.
16
            iframe.contentWindow.location.hash = window.location.hash;
17
          } else {
18
            // iframe is just starting.
19
            var newHash = window.location.hash;
20
            var srcStr = iframe.getAttribute('src');
21
            var words = srcStr.split('#');
22
            var href = words[0];
23
            var newSrc = href + newHash;
24
            iframe.setAttribute('src', newSrc);
25
          }
26
    };
27
28
    updateIframeHash();
29
    iframe.contentWindow.addEventListener("message", (event) => {
30
        if (event && event.data && event.data.hash) {
31
            window.location.replace(event.data.hash);
32
        }
33
    }, false);
34
    window.onhashchange = updateIframeHash;
35
}) ();
36