Bertjuhh
Bertjuhh

Reputation: 115

get javascript var from iframe and place in parent div

I got the following code:

<body>
<div class="header">
  <div class="logo">
     title of page goes here
  </div>
</div>
<iframe src='sameserver.html'>
     <script>
          var pagetitle = 'page title';
     </script>
</iframe>
</body>

Now how can i get the javascript var placed inside the logo div? Is it even possible?

Upvotes: 0

Views: 844

Answers (2)

Mihai Iorga
Mihai Iorga

Reputation: 39704

Ad ID to your logo, wait for iFrame to load and put variable to #logo

<body>
<div class="header">
  <div class="logo" id="logo">
     title of page goes here
  </div>
</div>
<iframe src="sameserver.html" name="myIFrame"></iframe>


<script type="text/javascript">
    window.myIFrame.onload = function (){
        title = window.myIFrame.pagetitle;
        document.getElementById('logo').innerHTML = title;
    }
</script>
</body>

and sameserver.html:

<script>
    var pagetitle = 'page title';
</script>

Upvotes: 3

Ashirvad
Ashirvad

Reputation: 2377

yes it is possible. You can do like this.

parent.document.getElementById('someid').innerHTML=pagetitle;

Upvotes: 1

Related Questions