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:
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
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
import {POP_UP_HEIGHT, POP_UP_WIDTH} from '../constants';
/**
* Handles interaction with the Office API for Common shared API.
*/
export default class Office {
constructor(office) {
this.office = office;
this.dialog = null;
this.callback = null;
}
/**
* Store the callback function that will be invoked to
* after pop-up message is received.
* @param {string} url
* @param {function} callback
*/
openPopUp(url, callback) {
this.callback = callback;
this.office.context.ui.displayDialogAsync(url,
{height: POP_UP_HEIGHT, width: POP_UP_WIDTH}, this.dialogCallback.bind(this)); // THIS IS WHAT YOU NEED
}
/**
* Send the message from the child window (pop-up window)
* To the parent window (Task pane window)
* @param {string} message
*/
messageFromPopUp(message) {
this.office.context.ui.messageParent(message);
}
/**
* The parent window will close the child window (pop-up)
* and invoke the callback functionality
* with a given message from the child window
* @param {Object} event
*/
dialogHandler(event) {
this.dialog.close();
this.callback(event.message);
}
/**
* Store the child window (pop-up window) and create
* an event handler to notify the parent window when
* the child window sends a message to it
* @param {Object} asyncResults
* @param {string} asyncResults.status Status of the result, preferably 'succeeded'
* @param {string} asyncResults.value
*/
dialogCallback(asyncResults) {
if (asyncResults.status === 'succeeded') {
this.dialog = asyncResults.value;
this.dialog.addEventHandler(this.office.EventType.DialogMessageReceived,
this.dialogHandler.bind(this));
}
else {
console.error(`Error: ${asyncResults.error.message}`);
}
}
}
来源:https://stackoverflow.com/questions/53027657/office-js-web-word-add-in-window-open-method-is-not-working-with-aboutblank