使用 Javascript 检索 AppScript 的 URL [英] Retrieve URL of AppScript using Javascript

查看:24
本文介绍了使用 Javascript 检索 AppScript 的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新参数重新加载当前页面,但无法检索网页的正确 URL.我得到一个备用网址,而不是触发网络应用程序的网址

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

这是我在 HTML 文件中的 javascript

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);
    }

这是被记录的 URL

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

如何检索地址栏中的实际 URL?

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

提前致谢!

推荐答案

问题:

具有不同来源的 iframe 无法读取父/顶级框架位置的 href 属性,该属性是只写的.所以,你不能.

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.

然而,您可以从服务器端传递 url.

You can however pass the url from server side.

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

客户端:

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

参考:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆