Hi friendly folks of the internet, quick scenario: I've got an application that uses subwindows for tooling, opened with window.open('/tools', '_blank'). It returns a window object that I can attach event listeners to, but when the user hits the refresh button, or the window performs a redirect (which mine does, because it's got a search function), the window object is destroyed, or at least I can't use it to get the current window. So my application thinks the window is closed, even though it isn't.
This becomes an issue when I need to use the window objects to exchange messages between the main application and the window. If the user has changed the window object, this won't work anymore. I'm just wondering if there's a better way to do this.
Obviously, I could have prompts built into the application with HTML, but I'm trying to avoid that for cleanliness reasons, and so the user can use their operating system's windowing system to make using the application easier.
In terms of code examples, there's not much to show, I have two scripts, one on the main, and one on the tool window,
main
$("#btn").on('change', function() {
const windowRef: Window | null = window.open('/tools', '_blank');
if (windowRef)
$(windowRef).on('load', function() {
windowRef.connect = function() {
// Initialise tools and functions here
};
$(windowRef).on('beforeunload', () => $("#btn").prop('checked', false));
});
});
tool window
...
<body>
<script defer>
new Promise((ok, err) => 'connect' in window ? ok(window.connect()) : err());
</script>
</body>