forked from thonatos/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# # gyro hack in iframe | ||
|
||
> If you use the gyro plugin in an iframe, it can only work if the domain of the main window is the same domain as the iframe. This is a (security) limitation of the browser. | ||
## #solution | ||
|
||
get gyro information in the main window & post it to the child iframe. | ||
|
||
- iframe | ||
- postMessage | ||
- JSON | ||
|
||
在主窗体获取传感器信息,并使用postMessage传递信息到iframe子页面,测试中发现postMessage需要的信息为字符串,所有先做一次JSON.stringify,在子页面做一次JSON.parse即可。 | ||
|
||
## #code | ||
|
||
``` | ||
//main window | ||
if (window.DeviceOrientationEvent) { | ||
var iframe_embed_hack = { | ||
enabled: true | ||
}; | ||
function onUpdate(){ | ||
document.getElementById('child').contentWindow.postMessage(JSON.stringify(iframe_embed_hack),"*"); | ||
} | ||
function onScreenOrientationChangeEvent(){ | ||
iframe_embed_hack.screenOrientation = window.orientation || 0; | ||
} | ||
function onDeviceOrientationChangeEvent(event){ | ||
iframe_embed_hack.deviceOrientation = event; | ||
onUpdate(); | ||
} | ||
window.addEventListener('orientationchange', onScreenOrientationChangeEvent, false); | ||
window.addEventListener('deviceorientation', onDeviceOrientationChangeEvent, false); | ||
} | ||
``` | ||
|
||
``` | ||
//the child iframe | ||
function messageHandler(data){ | ||
window.iframe_embed_hack = data; | ||
} | ||
window.addEventListener("message", function(ev) { | ||
messageHandler(JSON.parse(ev.data)); | ||
}, false ); | ||
``` |