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 Answer 1

4

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(However, In chrome/chromium, location.ancestorOrigins is provided, mainly so that advertisers can read the top origin, as chrome is message made by a advertising company with conflict of interests)

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:

Sign up to request clarification or add additional context in comments.

7 Comments

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 The ids of [ID]/dev and [ID]/exec are quite different. Is it possible to access the actual dev url?
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.
But, I'm not sure whether OP actually needs this. I apologize for this.
@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)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.