1

I have an iframe inside a web page, and I want to do something when it is ready. I tried 2 ways:

  • In the parent page, I use this

    jQuery('#iframe').load(function() {
     //my code here
    });
    

this code will be executed when the iframe is finished loading (as I wanted), but because my iframe is pointed to another domain, so at this point, I can't access its content (cross domain problem)

  • Inside the iframe, I use this:

     jQuery('body').ready(function() {
     });
    

This code is executed when the iframe starts loading, which is much earlier than I want, so the code can not function properly.

So how I execute code when the iframe is finished loading and ready for DOM-manipulation

I searched and found that easyXDM may be the solution, but I wonder if there is simpler solution exists.

Thank you.

OammieR
  • 2,800
  • 5
  • 30
  • 51
Delta76
  • 13,931
  • 30
  • 95
  • 128
  • Since it's cross-domain, it's never going to be ready for DOM manipulation unless you disable the cross-domain policy in your browser. – Dagg Nabbit Feb 28 '12 at 02:18
  • I'm a bit confused. `$(function() { });` should work perfectly fine inside the iframe. You cannot manipulate it from the parent document anyway. So it should already work with what you have. – Felix Kling Feb 28 '12 at 02:19

2 Answers2

0

dont't use jquery,It will make your javascript knowledge confusion,

You can use the following code

<script type="text/javascript">
    function show(){
        var iframe=document.getElementById('aa');
        var h=iframe.contentWindow.document.body.offsetHeight;
        alert(h)    
    }
</script>
</head>
<body>
<iframe id="aa" src="3.html" frameborder="0" scrolling="no" width="100%" height="200" onload="show()"></iframe>
</body>
  • 1
    From the question: *"my iframe is pointed to another domain"*... you cannot access the content of an iframe from another domain, regardless whether you use jQuery or not. – Felix Kling Feb 28 '12 at 04:53
0

Since you want to know when the iframe is ready AND you want to access content from that iframe cross-domain, I think easyXDM would be a good choice.

You could basically implement this example, and simply add your code to the "onReady" method.

JasonStoltz
  • 3,040
  • 4
  • 27
  • 37