Get current page URL

In a web application using the GAS HtmlService, I need the url of the current page to create a new link in the template.

The web application will run on the Google Sites page (GAS as gadget), but also autonomously

I try "var pageUrl = document.location.href" and then use pageUrl in the Html template as shown below. I know this is wrong, but I'm lost, Sorry.

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

Thanks Fausto

+5
source share
4 answers

You can get the url for webapp through ScriptApp:

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

and you can get the URL on sites through SitesApp:

 SitesApp.getActivePage().getUrl() 

although I don’t think this is really useful for what you seem to be describing, since you cannot add useful query parameters to it. Scripts are executed in an iframe, so although you can use document.location on the client, this will not help in any way. The URL is cleared and the service identifier is deleted.

+7
source

Can you afford the jQuery route?

 $(document).ready( function() { var currentURL = window.location; $('#myLink').prop('href', currentURL); }); 

Then...

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

Currently (since the change of sites in 2016) the answer is somewhat simpler:

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

this works for me:

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

then load the page of your web application and then check the log in your application!

0
source

All Articles