periodically report dom properties cross-domain with easy xdm

563 views Asked by At

I'm attempting to dynamically resize an iframe's height based on its content height, using easyXDM. The code makes a call to check the iframe's body.scrollheight every 5 seconds, the theory being if the page height has changed, the iframe height will update with it every 5 seconds. It works on the initial call - the iframe even resizes properly. The problem is that the provider continues to return the content's original height even when the iframe content's height has changed.

The code:

consumer (page containing the iframe):

var socket = new easyXDM.Socket({
remote: "http://remoteDomain.ca/blahblah.html",
container: "embedded",
onMessage: function(message, origin){
    alert(message); //continually alerts "542px" even when body.scrollheight 
                    // has changed
   document.getElementById('iframe').style.height = message + "px";  
   var t = setTimeout(function() {
      socket.postMessage();
   }, 5000);
},
onReady: function() {
    socket.postMessage();
}
});

provider (iframe content):

var socket = new easyXDM.Socket({
    onMessage: function(message, origin){
        socket.postMessage(document.body.scrollHeight);
    }
});
1

There are 1 answers

0
Daniel Oliveira On

After try easyXDM, it finally worked by using another sample: http://stevesouders.com/misc/test-postmessage.php

In source page just:

<script type="text/javascript">
    setInterval(function () {
        parent.postMessage(document.body.scrollHeight, '*');
    }, 500);    
</script>

In frame page:

<script type="text/javascript">

    if (window.attachEvent) {
        // IE
        window.attachEvent("onmessage", handleMessage);
    }

    if (window.addEventListener) {
        // FF
        window.addEventListener("message", handleMessage, false);
    }

    function handleMessage(e) {
        $("#frameId").attr("height", e.data);
    }

</script>
<iframe id="frameId" src="http://mysourceurl.com.br/" width="100%"
    frameborder="0" scrolling="no"></iframe>

Worked like a charm! My iframe height is refreshed every setInterval interation.