如何基于正则表达式在 O365 邮件应用程序中加载不同的页面

How to load different pages in O365 mail app based on regex

本文关键字:加载 应用程序 正则表达式 何基于 O365      更新时间:2023-09-26

我正在创建一个 O365 应用程序,我有 2 个.aspx文件,当用户单击 O365 邮件应用程序时,我希望根据邮件的主题加载这些页面中的每一个。

方案 1:邮件主题包含"#"结果:加载页 1

方案 2:邮件主题不包含"#"结果:加载页2

我尝试过有一个中间.js文件,我在其中编写了逻辑,但是当我做 window.location = "path_to_aspx_file"时,仅加载 HTML,但 JS 文件不运行。

我目前的实现:

我有着陆逻辑.js

(function () {
    "use strict";
     //The Office initialize function must be run each time a new page is loaded
    Office.initialize = function (reason) {
        $(document).ready(function () {
            var item = Office.cast.item.toItemRead(Office.context.mailbox.item);
            var sub = item.subject;
            if (sub.indexOf("some text") > -1) {                                
                window.location = "http://localhost:51776/File1.aspx";
            }
            else {
                window.location = "http://localhost:51776/File2.aspx";
            }
        });
    };
})();

经过一番摸索。我现在能够导航到这些文件中的每一个,但我不确定如何从 File1.aspx 和 File2.aspx 访问邮件主题。

在使用 Office

JavaScript API 获取主题之前,您是否初始化了 Office 上下文?为了轻松重定向 HTML 页面,我们可以包含如下所示的 JavaScript:

首页.js:

/// <reference path="../App.js" />
(function () {
    "use strict";
// The Office initialize function must be run each time a new page is loaded
Office.initialize = function (reason) {
    $(document).ready(function () {
        app.initialize();
        RedirectHTMLPage();
    });
};
function RedirectHTMLPage() {
    var subject = Office.context.mailbox.item.subject;
    if (subject.indexOf("#") != -1) {
        window.location.href = "https://localhost:44300/page1.aspx";

    } else {
        window.location.href = "https://localhost:44300/page2.aspx";
    }
}
})();

用于重定向的 HTML 页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<title></title>
<script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<link href="../../Content/Office.css" rel="stylesheet" type="text/css" />
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
<!-- To enable offline debugging using a local reference to Office.js, use:                        -->
<!-- <script src="../../Scripts/Office/MicrosoftAjax.js" type="text/javascript"></script>  -->
<!-- <script src="../../Scripts/Office/1/office.js" type="text/javascript"></script>  -->
<link href="../App.css" rel="stylesheet" type="text/css" />
<script src="../App.js" type="text/javascript"></script>
<link href="Home.css" rel="stylesheet" type="text/css" />
<script src="Home.js" type="text/javascript"></script>
</head>
<body>

</body>
</html>

我尝试过有一个中间.js文件,在那里我写了逻辑,但是当我执行 window.load = "path_to_aspx_file" 时,只加载了 html,但 js 文件不会运行。

您介意分享您使用"window.load"的详细信息吗?

飞雪的回答是正确的。 如果要从 File2.aspx 获取主题,请在 Office.initialize 事件中添加与 file1 相同的 Office.js 引用和访问主题.aspx

<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>