How to get ParentElement tag of an object?

I have an SVG graphic embedded via an object tag.

<!DOCTYPE html>
<html>
<head>
  <title>myTitle</title>
  <script type="text/javascript" src="script.js"></script>
  <link href="box.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id ="objectcontainer">
    <div id="displaybox" style="display: none;"></div>
    <object id = "mainSVG" type="image/svg+xml" data="map_complete.svg">
     <img id="svgPic" src="map_complete.svg" alt="Browser fail"/>
    </object>
</div>
</body>
</html>

There is a link in SVG:

<a id="emerBtn" xlink:href="emergency.html" onmouseover="return playVideo()" target="_parent">

The mouse over event should fire the following:

function playVideo(){
    //not working, all the time null
    var doc = document.parentNode;
    var elem = document.parentElement;
    var otherElem = document.documentElement.parentElement;
    //working if triggered from index.html
    var thediv = document.getElementById('displaybox');
    if(wasViewed == false) //show only one time
    {
        if(thediv.style.display == "none"){
            wasViewed = true;
            thediv.style.display = "";
            thediv.innerHTML = "<div id='videocontainer'><video autoplay controls style='display:block; margin-left:auto;" + 
                                "margin-right:auto; margin-top:150px; margin-bottom:auto; width:600px'>" +
                                "<source src='video.mp4' type='video/mp4'>HMTL5-Video not supported!</video>" + 
                                "</div><a href='#' onclick='return palyVideo();'>CLOSE WINDOW</a>";
        }else{
            thediv.style.display = "none";
            thediv.innerHTML = '';
        }
    } //close anyhow
    else{
            thediv.style.display = "none";
            thediv.innerHTML = '';
        }
    return false;
}

My problem is that I cannot access the "displaybox" from svg. I tried .parentNode, .parentElement, document.documentElement.parentElement etc. But all the time the parent / node is null.

Does anyone know how to access "external" HTML elements from a / svg object?

+4
source share
1 answer

SVG internally objectcreates a nested view context .

, :

function playVideo() {
  // ...
  var parentDoc = window.parent.document;
  var displayBox = parentDoc.getElementById('displaybox');
  // ...
}
0

Source: https://habr.com/ru/post/1584429/


All Articles