将变量从父级(我的域)传递到iframe(Google脚本)

时间:2019-03-05 08:32:15

标签: javascript parameter-passing postmessage

我正在尝试寻找将变量从父URL传递给子iframe(Google脚本)的方法。

我使用了Post消息方法,但是它不起作用,错误是:

  

删除postMessage ..来自主机https:// ....,但是预期的主机https://n-......script.googleusercontent.com

我的代码如下所示

父代码(JS):

window.onload = function() {
  // Get the window displayed in the iframe.
  var receiver = document.getElementById('Frame').contentWindow;

  // Get a reference to the 'Send Message' button.
  var btn = document.getElementById('send');

  function sendMessage(e) {
    // Prevent any default browser behaviour.
    e.preventDefault();

    receiver.postMessage('my message', 'https://script.google.com');
  }

  // Add an event listener that will execute the sendMessage() function
  // when the send button is clicked.
  btn.addEventListener('click', sendMessage);
}

我也尝试使用完整的Google脚本链接而不是“ https://script.google.comreceiver.postMessage('my message', 'full google script link');

Google脚本HTML代码

window.onload = function() {
  // Get a reference to the div on the page that will display the
  // message text.
  var messageEle = document.getElementById('message');

  // A function to process messages received by the window.
  function receiveMessage(e) {
    // Check to make sure that this message came from the correct domain.
    if (e.origin !== "my domain")
      return;

    // Update the div element to display the message.
    messageEle.innerHTML = "Message Received: " + e.data;
  }

  // Setup an event listener that calls receiveMessage() when the window
  // receives a new MessageEvent.

  window.addEventListener('message', receiveMessage);
}

经过数百次搜索,我发现了与通过Google Execution API从子级(Google脚本)到父级普通html页面的传递消息相关的线程,但无法理解其工作原理

如果与发布消息或其他方法有关系,可以将简单变量从我的域(常规html页面)传递到同一页面中的google脚本(iframe)。...

请帮助!!!

0 个答案:

没有答案