使用 Javascript 检索 AppScript 的 URL [英] Retrieve URL of AppScript using Javascript
问题描述
我正在尝试使用新参数重新加载当前页面,但无法检索网页的正确 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);
参考:
- 我的 iframe 在已发布的 Web 应用程序中的什么位置/边栏?
- 同源政策
- Service#getUrl
- google.script#run
这篇关于使用 Javascript 检索 AppScript 的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!