Tags
Asked 2 years ago
9 Jun 2021
Views 226
Palma

Palma posted

window.open() method is not working in Office.js

window.open() method is not working in Office.js
andy

andy
answered Apr 27 '23 00:00

If the window.open() method is not working in your Office.js add-in, it's possible that the method is being blocked by the Office.js sandbox environment, which is designed to prevent add-ins from executing potentially harmful code.

To open a new window in an Office.js add-in, you can use the Office .context.ui.displayDialogAsync() method instead. This method allows you to open a dialog window that can display content from a URL or HTML string.

Here is an example of how to use the Office. context.ui.displayDialogAsync() method to open a new window in an Office.js add-in:



function openNewWindow() {
    var url = 'https://www.example.com';
    var options = {
        height: 50,
        width: 50,
        displayInIframe: true
    };
    Office.context.ui.displayDialogAsync(url, options, function(result) {
        if (result.status === 'succeeded') {
            var dialog = result.value;
            dialog.addEventHandler(Office.EventType.DialogMessageReceived, function(args) {
                console.log('Received message: ' + args.message);
                dialog.close();
            });
        } else {
            console.error('Failed to open dialog: ' + result.error.message);
        }
    });
}

In this example, we define a function called openNewWindow() that uses the Office.context.ui.displayDialogAsync() method to open a new window with the URL https://www.example.com. We also specify the height and width of the window, and set displayInIframe to true to display the content in an iframe.

The displayDialogAsync () method takes a callback function as its third parameter, which is called when the dialog is closed. In this example, we add an event handler to the dialog that listens for the DialogMessageReceived event, and logs the received message to the console. We then close the dialog by calling the close () method.

If the displayDialogAsync () method fails to open the dialog, the callback function logs an error message to the console.

Keep in mind that the displayDialogAsync () method has certain limitations and restrictions, such as a limited size and the inability to access the parent window's DOM. Make sure to review the documentation to ensure that this method meets your specific requirements.
Post Answer