Office.js web WORD add-in: window.open() method is

2019-01-29 02:26发布

Summary:

window.Open('') or window.Open('about:blank') works with JavaScript in a normal html file as can be tested here. But it does not seem to work in an Office.js add-in.

Details:

On my Windows 10 desktop with VS2017 I created this Office.js WORD add-in project that works perfectly with my Microsoft Office Home and Student 2016 edition. In the same project, I then created a new button btnTest in the home.js file. When I click btnTest it successfully calls the following MyTest method and opens a new window with window.Open('some URL').

But in the same MyTest method when I call window.Open('about:blank') it does not open a blank page; instead, it opens windows 10 message box shown in screenshot below.

The goal here is that my code creates HTML string based on some content from WORD document and then use window.document.write(...) method to dynamically open that html in the browser as explained (and you can test) here. Question: How can I make window.Open('about:blank') method work?

function MyTest() {
        Word.run(function (context) {

            // window.open('https://www.google.com/'); this works
            var myWindow = window.open('about:blank'); //this does not work and, instead, displays the default Windows 10 message shown in screenshot below
            myWindow.document.write('<!DOCTYPE html><html><head></head><body><p>This is just a paragraph</p></body></html>');


            // Synchronize the document state by executing the queued commands,
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                //following (after un-commenting) does not work either
                //var myWindow = window.open('about:blank');
                //myWindow.document.write('<!DOCTYPE html><html><head></head><body><p>This is just a paragraph</p></body></html>');
            });
        })
            .catch(function (error) {
                console.log('Error: ' + JSON.stringify(error));
                if (error instanceof OfficeExtension.Error) {
                    console.log('Debug info: ' + JSON.stringify(error.debugInfo));
                }
            });
    }

Windows following message box pops up with OK button grayed out when window.open('about:blank'); is called:

enter image description here

NOTE: On the same desktop, I then created new this UWP AP with Javascript project and in the following method of their project, I un-commented their code and added window.open('about:blank');. When I call the following method there it successfully opens the default blank page there.

function sayHello() {
    //var messageDialog = new Windows.UI.Popups.MessageDialog("Hello, world!", "Alert");
    //messageDialog.showAsync();
    window.open('about:blank');
}

UPDATE:

How can I make it work from dialog? I tried the following but it did not work: I created this dialog Add-In. It works as it is. Then I commented out the code of submit() function in Form.js file of the add-in in the new project and added window.open('https://www.google.com/'); line instead. When I click the Submit button of the dialog, it successfully opens the google website in default browser. But if I replace the above line with

var myWindow = window.open('about:blank');
myWindow.document.write('html here'');`

it shows the same warning window shown in image above

1条回答
唯我独甜
2楼-- · 2019-01-29 02:48

We are blocking those calls at the sandbox level, i strongly recommend you to use the ShowDialog API as suggested here Is there any Office.js API to get user input

查看更多
登录 后发表回答