gpt4 book ai didi

javascript - 在新的谷歌网站中重定向表单

转载 作者:行者123 更新时间:2023-12-04 12:37:30 24 4
gpt4 key购买 nike

我使用应用脚本创建了一个表单。在 html 文件中,我有以下内容;

<form name="Subscribe-to-Central" id="Subscribe-to-Central" action="https://script.google.com/macros/s/key/exec" method="POST" onsubmit="myFunction()">  

Inputs ..

</form>

<script>
function myFunction() {
alert("Successfully subscribed. Please press okay to return to the home page");
window.open("URL", "_top");
}
</script>
该表单运行良好,可以将日期发送到附件并在提交后重定向到“URL”,但问题是当我尝试将表单嵌入到新的谷歌网站时,它仍然会将数据发送到表单但是不再重定向,并给出以下错误“script.googleusercontent.com 拒绝连接”。
PS:请注意,我只在新的谷歌网站上遇到这个问题。我尝试在经典的谷歌网站中嵌入相同的脚本,它工作得很好
Screenshot of the error
enter image description here

最佳答案

回答:
不幸的是,由于与经典站点相比,新站点的工作方式发生了变化,因此无法再在新站点中完成重定向。
更多信息:
正如您在控制台中看到的,在尝试从 JavaScript 导航顶级窗口时,您会收到以下错误:

Unsafe JavaScript attempt to initiate navigation for frame with origin https://sites.google.com from frame with URL https://<id>.script.googleusercontent.com/userCodeAppPanel. The frame attempting navigation of the top-level window is sandboxed, but the flag of allow-top-navigation or allow-top-navigation-by-user-activation is not set.


和:

Refused to display <URL> in a frame because it set X-Frame-Options to sameorigin.


可以设置 X-Frame-Options使用 .setXFrameOptionsMode() 的嵌入式 Google Apps 脚本页面 HtmlService的方法并使用 XFrameOptionsMode枚举器如下所示:
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
不幸的是,沙盒需要 allow-top-navegationallow-top-navegation-by-user能够从沙盒重定向的标志。根据 hte 文档, HtmlService 中唯一可用的沙盒模式是以下枚举数:
  • IFRAME : 使用 iframe 沙盒而不是 EMULATED 使用的 Caja 沙盒技术的沙盒模式和 NATIVE模式。自 2015 年 11 月 12 日起,此模式是新脚本和自 2016 年 7 月 6 日起所有脚本的默认模式。
  • NATIVE :建立在 ECMAScript 5 严格模式之上的沙盒模式。建立在 ECMAScript 5 严格模式之上的沙盒模式。此模式已于 2016 年 7 月 6 日停用。所有脚本现在都使用 IFRAME 模式。
  • EMULATED :一种遗留沙盒模式,仅使用 ECMAScript 3 中可用的功能来模拟 ECMAScript 5 严格模式。此模式是 2014 年 2 月之前的默认模式。实际上已弃用,所有脚本尝试使用 EMULATED现在将使用 IFRAME反而。

  • 沙盒嵌入的标志设置也无法在新站点界面中进行,因此也无法从站点端添加所需的导航允许标志。
    你可以做什么:
    只要您使用新站点,这里就没有什么可以做的。但是,正如您已经指出的那样,如果这是一个合适的解决方法,经典站点确实允许这样做。
    引用:
  • Same-origin policy - MDN web docs
  • The Document Object Model (DOM) - MDN web docs
  • Google Apps Script - Class HtmlOutput
  • Method HtmlOutput.setXFrameOptionsMode(mode)
  • Enum XFrameOptionsMode
  • Method HtmlOutput.setSandboxMode(mode)
  • Enum SandboxMode
  • 关于javascript - 在新的谷歌网站中重定向表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59823605/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com