【问题标题】:office-js: Uncaught TypeError: window.external.GetContext is not a functionoffice-js:未捕获的类型错误:window.external.GetContext 不是函数
【发布时间】:2016-01-28 22:41:36
【问题描述】:

我正在努力检查我的托管 Web 应用程序是由浏览器打开还是在 Outlook 2013/2016 客户端中打开。

这是我的方法:

/**
 * Check if the app is running in outlook
 */
$rootScope.isOutlook = false;
$(document).ready(function () {
    try {
        Office.initialize = function() {
            var hostType = Office.context.mailbox.diagnostics.hostName;
            if (hostType === "Outlook" || hostType === "Mac Outlook" || hostType === "OutlookWebApp") {
                $rootScope.isOutlook = true;
            }
        };
    } catch (e) {
        if (e instanceof TypeError) {
            // catch error if web app is opened in browser.
            console.log('Not in office context! Generated error: ' + e);
        }
    }
});
$log.debug('isOutlook: ' + $rootScope.isOutlook);

函数本身就像一个魅力,但我无法摆脱“未捕获的类型错误”。 它一直在我的浏览器控制台中向我抛出这个 Uncaught TypeError:

Uncaught TypeError: window.external.GetContext is not a function

【问题讨论】:

    标签: javascript office365 office365-apps office-js


    【解决方案1】:

    Office.context.mailbox.diagnostics.hostName 告诉您您拥有哪种类型的 Office 主机。但您似乎正在尝试在 Office 主机内和作为完全独立的网页运行相同的应用程序。

    有一个关于该场景的post by Simon J.K. Pedersen,其中涉及检查是否存在相同的 window.external.GetContext 函数。这没有记录。

     Office.initialize = init;
     try {
         if (!window.external.GetContext) {
             $rootScope.isOutlook = false;
             init(); // Manually call init
         }
     } catch (e) {
         // when in office context unable to access external.
     }
    

    您还可以在 Office 清单中包含查询字符串参数并检查它,以便您知道它来自 Office。

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多