0

I have stucked this issue for a month. Anyone who knows the solution, please help.

I would like to close all child window if the main window is closed (click 'X' button on tab or browser window) without any popup dialog. However, if the child window is closed by itself, there will be popup dialog asking about leaving page. So, I use localStorage to manage this scenario. (please take a look in the code below)

The problem is that any browser i.e. Internet Exlorer, Chrome works fine but Microsoft Edge (version 44) seems like that it doesn't set localStorage correctly. On MS Edge, it keeps displaying the dialog. I guess that this is an issue on MS Edge browser when set localStorage at unload event.

Anyone got the same issue? Thanks in advance for your suggestion and help.

Note: Before click "X" button at main window, please make sure that child window is dirty.

unload.html

<html>
<body>

<p>Login and Open child window:</p>

<button onclick="openWindow()">Login</button>

<p>Make child window dirty before close: </p>
<input type="text" />

<script>
var mywin;
localStorage.setItem("isOnline", "true");        

function setIsonlineFalse() {
  localStorage.setItem("isOnline", "false");
}

function getIsonline() {
  document.getElementById("isonline").innerHTML = localStorage.getItem("isOnline");;
}

function openWindow() {
  mywin = window.open('unload.html', 'child', 'fullscreen=no, left=10,top=10,width=700,height=700');
}

function closeWindow()
{
    if (mywin != null)
        {mywin.close();
    }
    return false;
}

window.addEventListener('beforeunload', function (e) { 
    console.log('isonline at before',   localStorage.getItem("isOnline"));
        if (localStorage.getItem("isOnline") == "true"){
            e.preventDefault(); 
            e.returnValue = ''; 
            return "Leaving this page will reset the wizard";
        }

}); 

window.addEventListener('unload', function (e) { 
    localStorage.setItem("isOnline", "false");  
    console.log('isonline at unload',   localStorage.getItem("isOnline"));
    closeWindow();

}); 


</script>
</body>
</html>
Andrew J
  • 11
  • 3
  • I can't reproduce the issue, localStorage seems to work fine in Edge in unload event too. – Teemu Apr 03 '20 at 04:22
  • @Teemu Please try to make child window dirty before close – Andrew J Apr 03 '20 at 05:01
  • @Teemu Enter something in the input. – Andrew J Apr 03 '20 at 05:28
  • I did, but localStorage is updated when closing the main window. Tested on Edge 44.18362.449.0 / 18.18363. – Teemu Apr 03 '20 at 05:31
  • @Teemu If localStorage is saved, there is pop-dialog on child window when it is force closed? – Andrew J Apr 03 '20 at 06:55
  • I'm not sure, if you need to call `closeWindow` at all in unload handler, the window is just about to be closed anyway (unload can't be cancelled). – Teemu Apr 03 '20 at 07:00
  • @Teemu Yes, I agree. When I debug at the line of calling closeWindow, I saw that localStorage is correct and MS Edge works fine. However, if I perform closeWindow without debugging, it seems that the child window doesn't get the updated localStorage. This make child window still pop-up leaving page which it shouldn't on MS Edge. – Andrew J Apr 03 '20 at 07:12
  • Did you try to check with any other version of the MS Edge legacy browser to verify that whether this issue occurred in a specific version of the Edge browser? As a workaround, you can try to use the MS Edge Chromium browser. – Deepak-MSFT Apr 03 '20 at 09:37
  • @Deepak-MSFT The version is irrelevant per se, since developers can't choose users' browser, though it could explain why I couldn't reproduce the issue. – Teemu Apr 03 '20 at 10:37
  • @Deepak-MSFT Thanks for your suggestion. MS Edge Chromium works perfectly. But the issue is what Teemu said. Users still use MS Edge legacy version. – Andrew J Apr 03 '20 at 15:17
  • I will try to provide feedback regarding this issue to Microsoft. Thanks for your understanding. – Deepak-MSFT Apr 08 '20 at 10:06

0 Answers0