[英]Detect orientation change from within cross-domain iframe
所以我有一個.jsp頁面,里面有一個iframe。 此iframe的內容托管在單獨的域中。 在移動設備上,我正在尋找這個iframe來檢測方向更改並相應地更改內容的尺寸。
有沒有辦法向存儲在此iframe中的內容添加事件監聽器,以檢測方向更改?
如果此內容不在iframe內,而是直接訪問,則調用如:window.addEventListener('orientationchange',FunctionToBeCalled,false); 成功捕獲方向更改並調用相應的功能。 但是,我似乎無法在iframe中使用它。 我已經嘗試過parent.addEventListener,parent.window.addEventListener,top.addEventListener等,但無濟於事。
從我讀過的內容聽起來好像是因為這是一個跨域調用,我可以使用非常有限的父功能,我注定要失敗嗎?
任何幫助將不勝感激。 謝謝!
如果您擁有父窗口的代碼,則可以使用跨文檔消息來解決此問題。 您可以將方向從父窗口發送到iframe。 這在跨域方案中也應該起作用。 我在iphone 4.3和android 2.2上測試了這個。
在父窗口中:注冊方向更改並將其傳輸到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)
有關詳細信息,請查看此內容: http : //html5demos.com/postmessage2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.