4

I want to know if the same URL is already opened in another tab of same browser,if so i need to raise a alert.Can any one help me with this.Need to do with javascript

  • 2
    possible duplicate of [Possible to detect if a user has multiple tabs of your site open?](http://stackoverflow.com/questions/4038629/possible-to-detect-if-a-user-has-multiple-tabs-of-your-site-open) – Nadh Apr 17 '15 at 10:41
  • you can use cookie and take care of it by handling window.onbeforeunload event. – webduvet Apr 17 '15 at 11:21
  • Hi @Nadh, the question is the same as in the possible duplicate, but the solution needed could be completely different given the oracle-apex enviroment – Typo Apr 17 '15 at 17:57
  • @Typo AFAIK, apex has no limitations in using javascript. – Dmitriy Apr 17 '15 at 19:53
  • @Dmitry I know, I'm not saying that the javascript solution won't work on oracle apex. What I meant was that oracle apex provide another set of tools (server sided) that could provide what the OP needs. – Typo Apr 17 '15 at 23:37

2 Answers2

1

Try this.

    var a = document.getElementById('opener'), w;        
   a.onclick = function() {
   if (!w || w.closed) {
    w = window.open("https://www.fb.com/");

  } else {
    console.log('window is already opened');

  }
  w.focus();
};
Arif
  • 927
  • 9
  • 9
1

Here is a working solution that uses plain JS and localStorage and updates the DOM with the count of the currently open tabs. It uses the storage event to keep the count synchronized across all open tabs/windows without any need for refreshing the page.

Note that this solution detects the number of open tabs/windows for a given domain within one browser, but does not maintain the count across different browsers. From the Mozilla docs on local storage:

[The localStorage event] is really a way for other pages on the domain using the storage to sync any changes that are made. Pages on other domains can't access the same storage objects.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
(function() {
    var stor = window.localStorage;
    window.addEventListener("load", function(e) {
        var openTabs = stor.getItem("openTabs");
        if (openTabs) {
            openTabs++;
            stor.setItem("openTabs", openTabs)
        } else {
            stor.setItem("openTabs", 1)
        }
        render();
    })
    window.addEventListener("unload", function(e) {
        e.preventDefault();
        var openTabs = stor.getItem("openTabs");
        if (openTabs) {
            openTabs--;
            stor.setItem("openTabs", openTabs)
        }
        e.returnValue = '';
    });
    window.addEventListener('storage', function(e) {
        render();
    })

    function render() {
        var openTabs = stor.getItem("openTabs");
        var tabnum = document.getElementById("tabnum");
        var dname = document.getElementById("dname");
        tabnum.textContent = openTabs;
        dname.textContent = window.location.host
    }
}());
</script>
</head>
<body>
<div style="width:100%;height:100%;text-align:center;">
    <h1 >You Have<h1>
        <h1 id="tabnum">0</h1>
    <h1>Tab(s) of <span id="dname"></span> Open</h1>
</div>
</body>
</html>
Neil VanLandingham
  • 1,016
  • 8
  • 15