All window need to close while user session expire How?

1k views Asked by At

I am trying to close window with session storage, its not working y ??

For me I open new window in different && different modules, so I cant maintain the "Var" for that I use session storage Globally for my application

<html>
<body>

<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>

<script>
var my1;


function openWin() {
my1 = window.open("", "myWindow", "width=200,height=100");
alert(my1)
sessionStorage.setItem('aa',my1);

}

function closeWin() {
var retrievedObject = sessionStorage.getItem('aa');
retrievedObject.close();
}
</script>

</body>
</html>

1

There are 1 answers

10
Yaser On

I created a working version in plunker for you here.

However you need to consider the points that if your code is running inside a tab and that gets closed, then your code will never run to close other windows and the user has to close them all.

BTW: your code won't work inside stackoverflow since it is sandboxed. (you don't have access to session storage inside it)

Update:

Because that my1 is a reference to an instance of the window, when you put it in sessionStorage, it will loose the references. So the only thing that comes to my mind is to store it locally. Look at the updated plunker.