0

I am trying to reload the current page with a new parameter, but I am unable to retrieve the correct URL of the webpage. I am getting an alternate URL instead of the one that triggers the web app

https://script.google.com/a/user/macros/s/A...ljhGC/dev?action=menu&id=1

when the button is clicked I want the action parameter's value to change and the page to reload as

https://script.google.com/a/user/macros/s/A...ljhGC/dev?action=checkout&id=1

Here is my javascript within the HTML File

console.log(document.url);

      var url = new URL(window.location.href);
      var query_string = url.search;
      var search_params = new URLSearchParams(query_string); 
      search_params.set('action', 'checkout');
       url.search = search_params.toString();
       var new_url = url.toString();
       window.location.replace(new_url);
       console.log(new_url);
    }

This is the URL that gets logged

https://n-ikwx...khq-1lu-script.googleusercontent.com/userCodeAppPanel?action=checkout

How do I retrieve the actual URL that is in the address bar?

Thanks in advance!

1 Answers1

2

Issue:

A iframe with different origin cannot read the href property of location of the the parent/top frame, which is write only. So, You can't.

Solution:

You can however pass the url from server side.

Server side:

function getTopUrl(){
  return ScriptApp.getService().getUrl();
}

Client side:

var topUrl;
function getTop(){
  google.script.run.withSuccessHandler((url)=>{topUrl=url;}).getTopUrl();
}
window.addEventListener('load', getTop);

References:

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Although I'm not sure about OP's situation, I thought that from the URL of `https://script.google.com/a/user/macros/s/A...ljhGC/dev`, OP might want to access to Web Apps with the developer mode. When `getUrl()` is used, how about adding the information to reflect the latest script? – Tanaike Jul 23 '19 at 00:39
  • @Tanaike The ids of `[ID]/dev` and `[ID]/exec` are quite different. Is it possible to access the actual `dev` url? – TheMaster Jul 23 '19 at 06:30
  • 2
    Thank you for replying. Yes. `{id}` of `https://script.google.com/a/{domain}/macros/s/{id}/dev` can be retrieved. In this case, it uses the method of projects.deployments.list in Apps Script API. For the response value from the method, the value of `deploymentId` in the 1st index of the property `deployments` is it. You can confirm it at [Try this API](https://developers.google.com/apps-script/api/reference/rest/v1/projects.deployments/list). – Tanaike Jul 23 '19 at 06:41
  • But, I'm not sure whether OP actually needs this. I apologize for this. – Tanaike Jul 23 '19 at 06:43
  • 2
    @Tanaike Ah yes the api. Possibly op doesn't need the dev url. Also, it's much easier to just publish the new app and get the exec url instead. Or even just copy/paste the dev url(I assume dev URL {id} doesn't change with change in source code) – TheMaster Jul 23 '19 at 06:46
  • @Tanaike That was my mistake. Copied the incorrect URLs they should be `/exec` URLs. – Sam Haberkorn Jul 23 '19 at 18:28
  • @Sam Haberkorn Thank you for your information. Can you update your question? It will be also useful for other users. – Tanaike Jul 23 '19 at 22:17