0

How can I check if the user closed the the browser tab that I opened and sent to a url?

Below is my code. When the user changes the value of a select dropdown it checks to see if the browser window was opened and if so reload the url if not open a tab to the selected url.

BUT if the user changes the select value and a new tab is opened then they close it on the next select change a new tab WILL NOT OPEN.

urlmenu.onchange = function() {
   if (typeof(ref) != 'undefined') {
     ref.location = this.options[ this.selectedIndex ].value;
   } else {
     ref = window.open( this.options[ this.selectedIndex ].value, '_blank');
   }
};

Let me know if you have any questions.

Denoteone
  • 4,043
  • 21
  • 96
  • 150
  • why not target a tab that way if it exists it will change if not it will open – Pete Sep 19 '14 at 15:03
  • [this may help](http://stackoverflow.com/questions/13779508/open-url-in-new-tab-or-reuse-existing-one-whenever-possible) – Pete Sep 19 '14 at 15:09

2 Answers2

1

Try this, this should do the trick

urlmenu.onchange = function() {

    var ref = window.open( this.options[ this.selectedIndex ].value, 'somename', '');

};

Instead of using '_blank' specify a name of the tab so that it will just refresh that same tab if it's open already and not create a new one.

Using '_blank' you issues a browser command to open another tab (or window if you spesicified hiehgt) check my example below

<button onclick="myFunction()">Try it</button>

<script>
var windowOpen = null;
function myFunction() {

    windowOpen  = window.open("http://stackoverflow.com", "somename", "");

}
</script>
andrex
  • 983
  • 5
  • 14
0

Set a name to your window:

urlmenu.onchange = function() {
    window.open( this.options[ this.selectedIndex ].value, 'urlmenutab');
};

If no window with this name, the browser while create a new one. If already exists, the browser re-use it.

Nicolas Albert
  • 2,586
  • 1
  • 16
  • 16