1

Below is the code which I am using in my HTML page. My requirement is,..

1) User when clicks on a a 'Open' button a new window with content should open. Now when user navaigates to someother page and comes back and clicks on the same 'Open' button then no new window should open as it is already open.

2) But if I close the opened window and then hit 'Open' button then new window should open.

I could achieve the first requirement with below code but not able to achieve the 2nd requirement with below code. I always get window.closed as false. Please help.

<script>

var win1;

// The if condition is for achieveing 1st requirement which is perfect

if(window.sessionStorage.getItem('isOpenOnce') == null)
{
window.sessionStorage.setItem('isOpenOnce', 'opened');
win1 = window.open("http://www.example.com");

}

// the below else if is for achieveing 2nd requirement which is failing.

else if(win1.closed)
{
win1.location.reload();
}

else {  
}

</script>
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Venkata Sunil
  • 11
  • 1
  • 2

1 Answers1

0

Use window.open/window.closed:-

    Wincheck = window.open( URL, name, features )
    if (Wincheck.closed) alert ("It's closed!")
    else alert ("It's still open!")

    if (parseInt(navigator.appVersion>2)) {
     if (Wincheck.closed) alert ("It's closed!")
     else alert ("It's still open!")
    }
abhi312
  • 364
  • 1
  • 6
  • 25