I am trying to open a new window when a button is clicked and print something in the new window. I've linked the button to the following 'switchToNewWindow' method:
windowOpen: false,
switchToNewWindow() {
if (!this.windowOpen) {
var win = window.open('', '_blank', 'location=no,titlebar=no,toolbar=no,top=200,left=350,width=1200,height=650');
this.windowOpen = true;
win.addEventListener('beforeunload', function() {
this.windowOpen = false;
}.bind(this));
}
},
The method as it is above, it's working, but if I want to change the listener as follows below, the code from the listener doesn't execute.
win.addEventListener('load', function () {
win.document.write("Hello, world!");
});
In the console while debugging I can see that the code doesn't enter the function in the second example. I should mention I tried also with 'DOMContentLoaded' instead of 'load', and still not working. My guess is that the event is not recognized? Any idea on why or what else is broken here? And why it is working with 'beforeunload'?
I have done this using a custom event. Maybe not the best approach but check if it works for your case.