问题描述
运行时:
MyPopWindow.postMessage(Test,' mydomaine ');
我在MyPopWindow whith script.google.com上发生错误:
$ b
运行时:
MyPopWindow.postMessage(Test,'') ;
MyPopWindow出现错误:
源页面 mydomaine :
window.addEventListener(DOMContentLoaded, function(){window.addEventListener(message,function(e){//等待孩子发信号表明它已经加载if(e.data ===loaded&& e.origin === iframe .src.split(/)。splice(0,3).join(/)){//向孩子发送一条消息alert(e.data);}})},false)$
以我的Google Apps脚本作为WebApp运行的源代码:
$ b
$ b
document.addEventListener('DOMContentLoaded',function(){//指示我们加载的父级。 window.parent.postMessage(loaded,*); //收听来自父母的消息。 window.addEventListener(message,function(e){if(event.origin!=='mydomain')return; var message = e.data; alert(message);},false);
解决方案
此错误讯息来自其中一个应用程式脚本。
我尝试了其他方法,例如传递URL参数,但至今都没有工作,因为我无法从所有应用程序脚本运行的iframe中访问它们。
When run: MyPopWindow.postMessage("Test", 'mydomaine'); I have a error on MyPopWindow whith script.google.com:
When run: MyPopWindow.postMessage("Test", 'https://script.google.com'); I have a error on MyPopWindow:
Source in page on mydomaine:
window.addEventListener("DOMContentLoaded", function() {
window.addEventListener("message", function(e) {
// wait for child to signal that it's loaded.
if ( e.data === "loaded" && e.origin === iframe.src.split("/").splice(0, 3).join("/")) {
// send the child a message.
alert(e.data);
}
})
}, false)
Source on my Google Apps Script runing as WebApp:
document.addEventListener('DOMContentLoaded', function () {
// signal the parent that we're loaded.
window.parent.postMessage("loaded", "*");
// listen for messages from the parent.
window.addEventListener("message", function(e) {
if(event.origin !== 'mydomain') return;
var message = e.data;
alert(message);
}, false);
});
This error message comes from one of App Scripts Javascript driver files.This is extra security Google added on to prevent people from using the postMessage system.
It seems Google wants to force you to use one of their accepted protocols for communication between windows/domains, namely the Execution API.I have attempted other methods like passing URL parameters, but so far none have worked because I could not access them from within the iframe that all apps scripts run in.
I believe the Execution API is your best bet.
这篇关于window.postMessage以script.google.com作为弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!