get current page url

2019-05-04 05:25发布

On a web app using GAS HtmlService I need to get the url of current page to build a new link within a template.

The web app will run in a Google sites page (GAS as gadget) but also standalone

I am trying "var pageUrl = document.location.href" and then use pageUrl within a Html template like follow. I know it's wrong, but I am lost, Sorry.

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

Thanks, Fausto

3条回答
老娘就宠你
2楼-- · 2019-05-04 05:40

Can you afford to go the jQuery route?

$(document).ready( function() {

var currentURL = window.location;

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


});

Then...

<a id="myLink" href="">Link to somewhere</a>
查看更多
成全新的幸福
3楼-- · 2019-05-04 05:44

You can get the url for a webapp via ScriptApp:

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

and you can get the url in sites via SitesApp:

SitesApp.getActivePage().getUrl()

although I don't think that's actually useful for what you seem to be describing since you can't add useful query parameters to it. Scripts run in iframes, so although you can use document.location on the client, it won't help you anyways. The URL is sanitized and the service id is stripped out.

查看更多
一夜七次
4楼-- · 2019-05-04 05:45

Nowadays (since the Sites change in 2016) the answer is a bit simpler:

var current_url = DocumentApp.getActiveDocument().getUrl();
查看更多
登录 后发表回答