Detect orientation change from within cross-domain iframe

后端 未结 1 789
我在风中等你
我在风中等你 2021-01-04 09:52

So I have a .jsp page that has an iframe in it. The content of this iframe is hosted on a separate domain. On a mobile device, I\'m looking for this iframe to detect an or

相关标签:
1条回答
  • 2021-01-04 10:43

    If you own the code of the parent window you can use cross document messsages to solve this. You could send the orientation from the parent window to the iframe. This should work as well in the cross domain scenario. I tested this on iphone 4.3 and android 2.2.

    In the parent window : register for orientation changes and transfer it to the iframe

    window.addEventListener("orientationchange", function(){
                    var iframe = document.getElementById("youriframe").contentWindow;
                    iframe.postMessage({
                        orientation: window.orientation
                    }, 'http://the.domain.of.the.iframe.com');
    }, false)
    

    In the iframe : subscribe to orientation changes from the parent

    window.addEventListener("message", function(e){
                var newOrientationValue = e.data.orientation;
                alert(newOrientationValue); // <--- DO your own logic HERE
    }, false)
    

    Check this out for more details : http://html5demos.com/postmessage2

    0 讨论(0)
提交回复
热议问题