如何从Web Word实例检索查询字符串值 [英] How to retrieve the Query string value from web word instance

查看:100
本文介绍了如何从Web Word实例检索查询字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为针对Word Online的Word创建一个Web加载项.当使用Word Online打开文档时,URL将包含一个文件名.我正在尝试检索该文件名.

I'm creating a web add-in for Word targeting Word Online. When a document is opened using Word Online, the URL will contain a file name. I am trying to retrieve that file name.

要检索此URL,我尝试使用window.location href,但它正在获取托管的插件Web应用程序URL.可以获取Word Online URL吗?

To retrieve this URL I tried using window.location href, but it is fetching my hosted addin web application URL. Is it possible to get the Word Online URL?

示例代码:

function getParameterByName(name, url) {
    if (!url) url = window.location.search;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }

推荐答案

应该使用什么?

您应该使用此处定义的Office.js API

What you should use instead?

You should use Office.js API that's defined here

https://docs.microsoft.com/en -us/javascript/api/office/office.document

Office.context.document.url这应该为您返回可以使用的东西.

Office.context.document.url this should return you something that you can use.

不幸的是,由于围绕iFrame的安全要求,因此您无法采用这种方式来实现它. Word Online正在使用iFrame托管您的加载项,除非特别允许,否则从iFrame访问父窗口通常受到严格限制.

Unfortunately this is not possible the way you're trying to implement it due to security requirements around iFrames. Word Online is using an iFrame to host your add-in and accessing the parent window from an iFrame is generally very restricted unless you're specifically allowed.

但是,您可以通过查看referrer标头来获取父窗口的基本主机url,仅此而已.我了解这对您来说还不够,这就是为什么我建议使用Word API的原因.

You can however get the base host url of the parent window by looking into the referrer header, but that's pretty much it. I understand that this is not sufficient for you, which is why I'm recommending to use the Word API.

您可以在下面看到有关iFrame的更多文档:

You can see a bit more documentation below about iFrames:

  • iFrame spec documentation: https://www.w3.org/TR/2011/WD-html5-20110525/the-iframe-element.html
  • On getting the url that's loading the frame (the parent URL): https://humanwhocodes.com/blog/2013/04/16/getting-the-url-of-an-iframes-parent/
  • Some more information on how to communicate between parent and the iFrame using postMessage API if you were whitelisted (which you aren't): https://medium.com/@Farzad_YZ/cross-domain-iframe-parent-communication-403912fff692

这篇关于如何从Web Word实例检索查询字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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