Check if a page is already opened - javascript

Is there a reliable way to know if an other instance of the same page is already opened in the browser?
I’ve tried setting up a variable in local storage at load and unload but mobile browsers do not always send the unload event..

The context of JavaScript code is per opened page (called window). Therefore, there is no reliable way to track open pages unless you open a WebSocket connection to the server and check the number of open pages by associating a unique identifier to each of them and prevent opening multiple pages. If you want to take this approach, search for a WebSocket implementation of your server-side application.

Related

Javascript sessionStorage Across tabs?

I am using sessionStorage to ensure an action only happens once per session. However, if a user opens a new window - the sessionStorage is not carried over to the new window, and the action is executed again. See my code below:
if sessionStorage.alreadyTracked != "1"
sessionStorage.alreadyTracked = "1"
...
Is there an easy way to do this that applies to all tabs? Maybe I need to use a cookie?
As far as html5 storage goes i would go for localStorage in your case.
See below for the differences.
Session Storage:
Values persist only as long as the window or tab in which they
stored.
Values are only visible within the window or tab that
created them.
Local Storage:
Values persist window and browser lifetimes.
Values are shared across every window or tab running at the same origin.
Read more here
You probably want to switch to localStorage as sessionStorage is bound to individual tabs and localStorage is not.
If you're OK with targetting only FF/Chrome you might also want to take a look at the Broadcast Channel API
The Broadcast Channel API allows simple communication between browsing contexts (that is windows, tabs, frames, or iframes) with the same origin (usually pages from the same site).

Javascript. Check if there is no tab in focus

Now i'm using visibilitychange event to see which browser tab is active. I use active tab to watch some events on server side, and I don't want to run this watcher in all open tabs.
But now I want to run this watcher even if no tab is in focus, but still, in just one tab.
How to check if no tab is in focus and ensure that some script is running in only one background tab?
You have to solve the issue of communicating between tabs. There are several options:
Cookies
Local storage
Server side syncing (WebSockets, Ajax pooling, etc)
Each tab will have to update its state in the shared data centre (one of the above).
Once that is done, you should use a master selecting algorithm to decide which one of the tab should communicate with the server. In your case, this could be as simple as the first opened tab.
There are some quirks when using local storage such as two different sub-domains can't access the same local storage. Cookies are another way around where you can define parent domain as well. I would say the third option is the best in terms of debugging and adding more complex selection logic.

How can I use postMessage to share data between top level windows?

Using Web Messaging (postMessage), it's easy to send messages between windows including iframes and popup windows opened through Javascript. However, is it possible for two windows opened individually by the user to find each other and communicate?
As an example, if the user opens www.example.com/app.html in the browser, then the same page in another tab/windows of the same browser, I want the second window to know that it should act as a "child" of the first one and exchange a stream of events via postMessage. How do I detect the presence of another open window and how to I get a handle to it that I can use with postMessage?
i don't know if it's possible with postMessage.
but, it should be possible with localStorage or sessionStorage (which lives in the session scope).
using this approach you can write a value in one window/tab, and read it in the other window/tab, of course assuming that it's all on the same domain.
see more here: http://php-html.net/tutorials/html5-local-storage-guide/
hope that helps.

accessing other tab from one window

How does the browser treat multiple tab? Are they completely separate entity where no interaction is possible? I understand the sandbox concept of the browser and other security concerns but is it possible for a webpage to interact with another tab in the browser?
Basically my question is: If a user loads one webpage in a new tab, is there some way to access information of other tab which is already opened or will be opened after?
I have one concept of an application which needs to know about the other tab already opened or opened after my conceptual webpage but I don't know if this is possible.
As far as I know, this isn't possible. The browser wouldn't allow you to manipulate the browser's lower functions in a regular environment. It would ignore it or show a security error come up.
I think there is no way to do that, except when both documents are written to communicate with each other (Like in vBulletin new windows). The only way to access tabs is writing Add-Ons for the browser.
There is no way to access other tabs on the client-side.
However, I can imagine a scenario in which this could be done server side. Have the user log in to your site on both tabs and use something like sockets to pass data back and forth from one tab to the other using the server as a middle-man.
If both pages are from the same domain, you can use cookies or, in HTML5, local storage.
If you own the other tabs, you can broadcast to other tabs, and other tabs can broadcast back to your tab, creating a practical communication channel among them.
This is called Inter-window messaging, and it uses LocalStorage.
To simply check if you are the active tab, use $(window).blur( ... ), or a similar technique using a library of your choice.

js communicate between popup to main window

I got two browser windows on the same domain,
one is the main window and the second is small popup window.
i found this: How to Communicate between two browser windows?
but, the problem is the popup opens as event on the local computer by 3rd party software...
and i don't wish to communicate back to the server, and reading the status at the main window, for slow time issues..
i wish to transfer some data from the popup directly to the main window via JS (and close the popup right after).
the event is a VOIP new income call opened by the local phone soft dialer with parameters, and the main window is a browser CRM that will need to show incoming call status via JS on the same page, AJAX-like [only local].
p.s:
maybe there is a way to communicate between browser to windows application?, so the 3rd party software will send data to it and the application will communicate to the window
(or Firefox extension - but i prefer without the need to install more addons)...
what approach should i take? what do you think is the solution?
thanks allot. ;)
If the one browser does not open up the other browser, there is no way for the two browsers to talk through window.opener.
What you could try is storing data into localstorage and have the windows poll localstorage for changes.
Have you tried using window.opener to refer to the parent window?

Categories