-3

There was a task to make each web page of the project not repetitive. Those. the user should not, as a consequence of inattention within the same browser, open multiple duplicates of the same web page. Please suggest a specific solution.

iliaschet
  • 1
  • 3
  • on that page set some key in session storage and onpage load check if the key already present in session storage then redirect it to other page ( A quick Idea :P) But you ll also need to delete that key on some certain condition – Ans Bilal Jul 03 '19 at 07:57
  • Possible duplicate of [Communication between tabs or windows](https://stackoverflow.com/questions/28230845/communication-between-tabs-or-windows) – Bartu Jul 03 '19 at 07:58
  • What about diff browsers, or devices? Or is that ok? – Lawrence Cherone Jul 03 '19 at 07:59
  • 1
    @AnsBilal , that's right! But localStorage – iliaschet Jul 03 '19 at 08:08

1 Answers1

-1

The task can be solved very simply, if one condition is met — each page has its own unique identifier. I have this $(‘body’).attr (‘id’). Let’s call the function noDuplicateTabs and call it every time the page starts. We use localStorage:

let noDuplicateTabs = function (pageName) {
  localStorage.getItem(pageName)
  ? window.close()
  : localStorage.setItem(pageName, 'open');

  window.onbeforeunload = function () {
     localStorage.setItem(pageName, '');
  };
}

Thus, we pass the name of the page in the function, check for the presence of a value using the localStorage key corresponding to the page name. If a key with a non-empty value is found, then we close the page, if not, then write the value to it ‘open’.

We also use the onbeforeunload property of the window, which works before reloading or closing the page. In this case, we will erase the value. Now there will be no duplicates within the same browser.

iliaschet
  • 1
  • 3
  • Session storage lives within one tab and if the user opens another one, then she will not be able to use the same Session storage. – iliaschet Jul 03 '19 at 08:06