Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference a DIV from iFrame inside it

Status
Not open for further replies.

peter321

Programmer
Sep 16, 2005
3
SI
I have a DIV containing iFrame. iFrame contains some HTML. At some point I need to reference outer DIV. I don't know id or name of the DIV or iFrame. Is this possible? I think there should be a way to crawl up the DOM tree from current position.

I know this I can do this by server side scripting but I would rather do it this way if possible.

Thanks for your help
Pet
 
Yes, it is all on the same server, same domain, same project.
 
Quick test produced this:

Code:
<html>
<body>
<div id='notMe'>Hello, World!</div>
<div id='guessMyName'><iframe src='page2.html'></iframe></div>
</body>
</html>

Notice the above has two DIVs. One has an iframe. In this case, the src of the iframe is known ahead of time.

Here's the HTML from the inner page:

Code:
<!-- page2.html -->
<html>
<head>
<script>
function findMyDiv()
{
 var divArray = parent.document.getElementsByTagName("DIV");
 var i=0;
 var myDiv = null;
 OUTER:for(; i<divArray.length; i++)
 {
  for(var j=0; j<divArray[i].childNodes.length; j++)
  {
   if(document.location.href.indexOf(divArray[i].childNodes[j].src) > -1)
   {
    myDiv = divArray[i]; //found it!
    break OUTER;
   }//end if
  }//end for
 }//end for

 if(myDiv)
  alert("I'm in " + myDiv.id);
 else
  alert("I'm lost!");
}//end findMyDiv()
</script>
</head>
<body onload='findMyDiv();'>
Back at ya'!
</body>
</html>

This looks at the page which contains it, goes through the DIVs on that page, checks the childnodes of those divs for one with a src attribute which matches this page's URL, deduces that this is its mother-DIV, and saves that DIV to a var for use in whatever way it likes. Here, it just finds the id-attribute of that div and displays it in an alert message.

Is this along the lines of what you're looking for?

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top