Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the ParentElement of Object Tag?

I have a SVG graphic embedded via 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>

In the SVG is a link:

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

The mouse over event should trigger 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 the svg. I tried .parentNode, .parentElement, document.documentElement.parentElement etc. But all the time the parent element/node is null.

Does anyone know how to access the "outer" HTML elements from the object/svg?

like image 293
Maik Lindemann Avatar asked Nov 09 '22 15:11

Maik Lindemann


1 Answers

An SVG inside an object creates a nested browsing context.

To access elements outside this child document, you need to access the parent document:

function playVideo() {
  // ...
  var parentDoc = window.parent.document;
  var displayBox = parentDoc.getElementById('displaybox');
  // ...
}
like image 118
joews Avatar answered Nov 14 '22 21:11

joews