告诉iFrame的内容方向

时间:2012-01-17 10:31:36

标签: javascript iframe cross-domain orientation

我目前有一个网站,其中包含专为平板电脑设备(例如iPad)设计的iFrame。 我能够在父级中检测方向以更改iFrame本身的宽度和高度,但我似乎无法通过iFrame页面检测更改时设备的方向。

我知道如果我在Safari Mobile中加载iFrame本身的实际页面而不是在iFrame中,它会检测更改并根据当时的方向旋转。 如果我在iFrame中加载横向页面时,它会以横向方式加载内容,反之亦然。 它似乎不希望在iFrame中加载自身后不断检测方向,仅在初始加载时。

有没有办法告诉iFrame方向在某种程度上有所改变?我使用过这段代码,但没有成功:

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

更难的是我无法编辑iFrame的内容,因为它不是我的!下面的代码就是您要注入的代码!

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

请帮忙,我这几天一直绞尽脑汁! 希望一切都有意义。

0 个答案:

没有答案
相关问题