【问题标题】:Redirecting to another Google Script webapp after button click单击按钮后重定向到另一个 Google Script webapp
【发布时间】:2019-12-04 13:20:00
【问题描述】:

我正在尝试在单击按钮后将 google app script web app 重定向到另一个 app script。我尝试了以下方法:

<button onclick="myFunction()">Replace document</button>
<script>
function myFunction() {
  location.replace("https://www.w3schools.com")
}
</script>

错误是谷歌脚本拒绝连接。检查检查元素时,它会将“X-Frame Options”显示为“same-origin”。

有解决办法吗?

我可以使用标签,但如何在警报后显示它?

【问题讨论】:

  • 出于安全原因,您不能在浏览器中使用location.replace 执行此操作。 developer.mozilla.org/en-US/docs/Web/API/Location/replace 解释说:“抛出 SECURITY_ERROR 类型的 DOMException。如果调用该方法的脚本的来源与 Location 对象最初描述的页面的来源不同,则会发生这种情况......”
  • 有没有办法在提醒后显示链接?
  • @Cat 在这种情况下,位置对象和调用替换的脚本来自同一个域。您可以在浏览器中执行此操作。

标签: javascript html google-apps-script web-applications


【解决方案1】:

问题:

  • 尝试在 iframe 中加载 https://www.w3schools.com:请注意,您的脚本是由 Google 在不同来源的沙盒 iframe 中提供的。这里的location 指的是https://*-script.googleusercontent.com 托管在https://script.google.com 内部的沙盒iframe @https://*-script.googleusercontent.com。当您替换 location 时,您正在将内部沙盒 iframe 替换为 w3schools 网站。现在,w3schools 不想进入script.google.com 或任何其他网站。因此,它将X-Frame Options 设置为same-origin。有些网站可以接受。它们可以嵌入。 w3schools 不是其中之一。
 =============  
 |GASWebApp  |<---script.google.com[Top Frame]
 |           |  
 |=========  |  
 ||SandBox|  |  
 ||User   |<-|---- Where your html code is
 ||Frame  |  |    (*.googleusercontent.com) 
 |=========  |     [Sandboxed iFrame]
 |           |  
 =============  

解决方案:

  • 在顶部框架中加载网页

片段:

window.top.location.replace("https://www.w3schools.com")

参考资料:

【讨论】:

    【解决方案2】:

    这样的东西会处理您的用例吗?

    var trigger = document.getElementById("trigger");
    var link = document.getElementById("link");
    
    trigger.addEventListener("click", showMessageAndThenShowLink);
    
    function showMessageAndThenShowLink(){
      alert("You are about to see the link");
      link.style.display = "inline";
    }
    #link{ display: none; }
    <button id="trigger">Show Message</button>
    <br />
    <a id="link" href="https://www.w3schools.com">www.w3schools.com</a>

    注意:在 SO sn-p 中,单击链接会出现“拒绝连接”错误,但在我的桌面上运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2019-09-15
      • 1970-01-01
      • 2014-07-08
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多