从iframe获取javascript var并将其放在父div中

时间:2012-08-28 09:45:19

标签: javascript jquery iframe

我收到了以下代码:

<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>

现在我怎样才能将javascript var放在logo div中? 它甚至可能吗?

2 个答案:

答案 0 :(得分:3)

广告ID到您的徽标,等待iFrame加载并将变量放到#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>

sameserver.html

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

答案 1 :(得分:1)

是的,这是可能的。你可以这样做。

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