获取当前页面网址

时间:2013-01-03 03:47:15

标签: google-apps-script

在使用GAS HtmlService的网络应用中,我需要获取当前页面的网址以在模板中构建新链接。

网络应用程序将在Google网站页面(GAS作为小工具)中运行,但也可以单独运行

我正在尝试“var pageUrl = document.location.href”,然后在Html模板中使用pageUrl,如下所示。我知道这是错的,但我迷路了,抱歉。

<a href="<?= pageUrl + "?item=xyz" ?>" >Item xyz link</a>

谢谢,Fausto

4 个答案:

答案 0 :(得分:6)

您可以通过ScriptApp获取网络应用的网址:

<a href="<?= ScriptApp.getService().getUrl() + "?item=xyz" ?>" >Item xyz link</a>

您可以通过SitesApp获取网站中的网址:

SitesApp.getActivePage().getUrl()

虽然我不认为这对你所描述的内容有用,因为你无法向它添加有用的查询参数。脚本在iframe中运行,因此尽管您可以在客户端上使用document.location,但它无论如何都无济于事。该URL已被清理,服务ID被删除。

答案 1 :(得分:0)

你能负担得起jQuery路线吗?

$(document).ready( function() {

var currentURL = window.location;

$('#myLink').prop('href', currentURL);


});

然后......

<a id="myLink" href="">Link to somewhere</a>

答案 2 :(得分:0)

现在(自2016年更改站点以来),答案要简单一些:

var current_url = DocumentApp.getActiveDocument().getUrl();

答案 3 :(得分:0)

对我有用:

function doGet(e){
  Logger.log(ScriptApp.getService().getUrl());
}

然后加载您的Web应用程序页面,然后检查您的应用程序脚本中的日志!