سؤال

I have a JavaScript application in a tab which opens if I click on a certain link on my page. If I click on the link again, I need to check if that tab is allready opened - if it is, then switch to that tab and do something, otherwise open a new tab.

Unfortunately using

var myApp = window.open("http://www.mypage.com/myapp.html", "My App");
if(myApp){

won't help me, because I can't rely on the URL, due to the fact that the URL will differ on each environment that I'm using.

How can I achieve this if I can't rely on the URL? The only thing I can check is the tab's name - it will allways be the same. How can I access the browser's tab array, please?

Thanks!

هل كانت مفيدة؟

المحلول

As far as I understand, you cannot access the browser's tab array (certainly not through JavaScript). This would be a security breach on the client side. You should think of each browser tab as a browser instance of it's own - unaware of any other tab.

نصائح أخرى

You can track that if tab is opened already using the Cookie/Local storage. Save the value "isOpened=true" in either Cookie or localStorage and Be sure to delete the values from the same when that tab is Closed or when user navigates to other pages using "onunload" event.

But the page you are going to open should be on the same Domain for accessing/ setting the Cookie.

Still we cannot do anything if the user opens the page by just copying the link.

as you are opening new window using javascript you can do this
you can add a dummy attribute say "data-isOpend='no'" on that element
and when user click on that element you can change that attribute to "data-isOpened='yes'"
and in javascript function before opening tha new window you have to check if data-isOpend=='no' then open link in new window else do nothing

(Just leaving a differenct ans here if any other dev comes here looking for solution for such prob,Just like me :) )

We can assign name to the tab while creating it For eg.

function view_preview()
{
  var abc;
  if(abc!=null){
    abc.close();
  }
  var url=some_link
  abc=window.open(url,'tab_name');
}

Here, we are opening a new tab and if that tab is already opened we are 'refreshing' it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top