maciekm
maciekm

Reputation: 257

prevent opening another the same tab in browser

Let's say I have main.php and page1.html on my www apache server. Part of main.php:

 ...
 <script> 
   function openNewTab(){
   var win=window.open('page1.html', '_blank');
       win.focus();
   };
 </script>
 ... 
 <?php
  echo  "<p><a href='' onclick='openNewTab(); return false;'>start</a></p>";
 ?>

When I click link above, I have opened two tabs: main.php and page1.html. And my aim is to prevent user opening another tab with page1.html, when he press link on main.php. Or , when user click link on main.php, tab with page1.html should be refreshed, but new tab shouldn't be opened.

thanks in advance.

Upvotes: 0

Views: 3369

Answers (2)

Tzar
Tzar

Reputation: 1819

You can try it like this:

 <script> 
   function openNewTab(){
   var win=window.open('page1.html', 'mypage1');
       win.location.reload();
       win.focus();
   };
 </script>

But now, this doesn't guarantee that it'll open in a new tab only. This will depend on the user's browser settings. By default it opens in a new tab, so it might work out for you!

Check and see if it helps!

Upvotes: 0

Luke Joshua Park
Luke Joshua Park

Reputation: 9805

You can't control a browsers behaviour, and if you could, it would be highly frowned upon. The user should always have full control over what tabs open where. You could actually achieve this using AJAX and Cookies but mobile compatability would be a big headache.

I would recommend finding a way to solve your problem that keeps the user in control of their browser.

Upvotes: 1

Related Questions