开发者

Display iframe div contents on parent page

开发者 https://www.devze.com 2023-04-07 09:31 出处:网络
I\'m sure this is pretty basic, but I can\'t seem to get any of the solutions I\'ve found to work for me. Basically, I need to get the contents of a div from an iframe, and write them to a div in the

I'm sure this is pretty basic, but I can't seem to get any of the solutions I've found to work for me. Basically, I need to get the contents of a div from an iframe, and write them to a div in the parent page. This is what I have:

<iframe src="slideshows/slideshow/case2.html" frameborder=0 scrolling="no" 
id="frame1" name="frame1"></iframe>

<div id="caption">
   <script type="text/javascript">
   var caption = frame1.document.getElementById('slidecaption');
   document.write(caption);
   </script>
</div>

'slidecaption' is the name of Id in the iframe that I'm trying to grab.

All I get is "null". Is it because the iframe contents haven't loaded yet? If so, how do I delay until the iframe has loaded?

Thanks, Scott


Thanks to both of you for your help. I figured it out, based on Márcio's idea:

I put the function in the parent page:

<script type="text/javascript">
function send() {
document.getElementById('slidecaption').innerHTML = 
frame1.d开发者_如何转开发ocument.getElementById('slidecaption').innerHTML}
</script>

And I put the call in the iframe document:

<body onload="parent.send();">

Regards, Scott


I'm not sure this is the only thing you need to do, but you certainly need to retrieve frame1 from the dom.

<script type="text/javascript">
   var frame1 = document.getElementById('frame1');
   var caption = frame1.contentWindow.document.getElementById('slidecaption');
   document.write(caption);
 </script>
0

精彩评论

暂无评论...
验证码 换一张
取 消