You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script站点如何实现无需点击链接的自动页面跳转?

问题原因

Refused to display 'https://google.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

该报错的根本原因是Google Apps Script部署的Web应用默认会将自定义HTML内容嵌套在谷歌域名的iframe中渲染,直接调用window.location.href修改的是iframe内的窗口地址,而google.com设置了X-Frame-Options: SAMEORIGIN响应头,禁止被非同源页面嵌套加载,因此跳转失败。手动点击链接可正常跳转,是因为<base target="_top">配置让所有a链接默认在顶层窗口打开,脱离了iframe限制。

可用解决方案

方案1:直接操作顶层窗口跳转

将JS跳转逻辑的作用对象改为顶层窗口,和a标签的target="_top"效果完全一致,代码示例如下:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
window.top.location.href = 'https://google.com';
</script>
</body>
</html>

方案2:JS模拟点击现有a标签

如果页面已经存在可正常跳转的a标签,也可以直接模拟点击该标签,复用已有的跳转配置:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<a href="https://google.com" id="jumpLink">link</a>
<script>
document.querySelector('#jumpLink').click();
</script>
</body>
</html>

两种方案都不需要用户手动操作,页面加载完成后会自动完成跳转。

内容的提问来源于stack exchange,提问作者kensuke1984

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 20:30:02