#Returning HTML containing window.close() on a new window closes all windows

13 messages · Page 1 of 1 (latest)

vestal hare
#

I'm creating a new window that opens to a URL, after which I want to close the window. I can close it via a Rust window handler but I wanted to try returning <script>window.close()</script> from the URL, thus making it close automatically, but that closes the main window too. Is there anything I could do about that?

dull tangle
#

There's also window.hide() (I know that's not the same but just-in-case that helps)

#

What about (js)

const win2 = WebviewWindow.getByLabel('win2');
win2.close();
vestal hare
#

oh no that's not what i meant

#

i meant that i was opening a new webview window that would go to a URL, and that URL returned the following HTML:

<h1>...</h1>
<script>window.close()</script>

the issue is that this closes all the windows, and i'm wondering if i could make it so only the window that receives said HTML gets closed

#

sorry, it's a weird thing to explain and my explanation was pretty poor

dull tangle
#

I'm pretty sure my suggestion was exactly what you want. When you create a window you should be providing a label. You need this label to get a reference to that window like so:

<script>
const win2 = WebviewWindow.getByLabel('myWindow'); // Replace myWindow with your window's label
win2.close();
</script>
dull tangle
#

@vestal hare
Okay, I think I see what you actually mean.

<script>emit("CloseWindowEvent")</script>

In each window:

unsub = listen("CloseWindowEvent") {
    // some how track which window I am here
    // Maybe pass the window reference?
    emit("CloseMe", { myWindow: window })

   // Or maybe even just window.close(); would work here.
}

Then make a close manager

unsub = listen("CloseMe", (e) => {
e.payload.myWindow.close();
})

What about this? Maybe there's a this.Close();

vestal hare
#

yeah maybe this could work

#

although i think my current solution of just holding a reference to the opened window and closing it after it's no longer needed is simpler

#

i don't think it's worth the trouble really, i was just looking for a nicer solution to what i have

#

thanks though!

dull tangle
#

window.getCurrent().close();
@vestal hare What about this?

Apparently you can also window.appWindow().close();?