如何阻止重定向的 URL 被屏蔽? [英] How to stop redirected URL from being masked?

查看:40
本文介绍了如何阻止重定向的 URL 被屏蔽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 google 应用程序脚本(附加到 google 文档)中有一个 doGet 函数,它作为网络应用程序发布,我希望用户被重定向到另一个网页.重要的是,我希望他们被重定向的页面的 URL 显示在地址栏中,他们被重定向的页面的标题是选项卡的标题(在 Chrome 中).

I have a doGet function in my google apps script (attached to a google doc) which is published as a web app and I want the user to be redirected to another web page. Importantly, I want the URL of the page they are redirected to be displayed in the address bar, and the title of the page they are redirected to be the title of the tab (in Chrome).

我试过使用元刷新标签,并设置window.location.href.这两个重定向都正确,但它们在地址栏中显示地址应用程序的 URL,而不是用户重定向到的页面的 URL.

I've tried using a meta refresh tag, and setting the window.location.href. Both of these redirect correctly but they show the URL of the address app in the address bar, not the URL of the page the user is redirected to.

下面附在 Google Doc 上的脚本说明了这个问题.

The below script, attached to a Google Doc, illustrates the problem.

function doGet(request) {
  var drive = DriveApp;
  var docs = DocumentApp;
  var Id = docs.getActiveDocument().getId();
  var document = docs.openById(drive.getFileById(Id).makeCopy().getId());
  document.setName("Test doc 2");
  var URL = document.getUrl();
  return HtmlService.createHtmlOutput('<meta http-equiv="refresh" content="0; url=' + URL + '" />')
}

将脚本发布为 Web 应用程序然后访问 URL 会将您重定向到新创建的文档,但地址栏中显示的是脚本的 URL.

Publishing the script as a web app and then visiting the URL redirects you to the newly created document, but it is the URL of the script that displays in the address bar.

请参阅此 Google 文档以获取示例:https://docs.google.com/document/d/1HpBkNGGGjKj3W6QXThtGdniSO_UTANo8LcqmgZowdTQ/edit

See this Google Doc for an example: https://docs.google.com/document/d/1HpBkNGGGjKj3W6QXThtGdniSO_UTANo8LcqmgZowdTQ/edit

推荐答案

由于您的 html 加载到内部 iframe 中,您应该使用

Since your html is loaded in a inner iframe, You should use

window.top.location = url

加载到顶部框架中.

这篇关于如何阻止重定向的 URL 被屏蔽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆