0

I am working on an app where a user can edit a "thing". I don't want multiple users to edit the same thing at the same time, so I've implemented a "locking" technique. It seems to be working fairly well. If you try to open a thing that is locked by someone else, you can view the thing, but in read-only mode.

To accomplish this, I am using window.sessionStorage, like this:

function sessionNumber() {
    if (window.sessionStorage.sessionNumber) {
        return window.sessionStorage.sessionNumber;
    }
    else {
        var num = Math.floor(Math.random() * 100000000 + 1);
        window.sessionStorage.sessionNumber = num;
        return num;
    }
}

In chrome, if you open the same "thing" in a different tab, you get a different session number. In Opera, if you open the same thing in a different tab, you get the same session number.

To be clear, I prefer Chrome's behavior over Opera's. Is there a cross browser way of accomplishing this?

George Mastros
  • 24,112
  • 4
  • 51
  • 59
  • Use `localStorage` instead of `sessionStorage`. – Barmar Apr 21 '23 at 16:34
  • 2
    But how does this lock between different users? Browser storage is specific to that user. If you want something to coordinate between users it has to be on the server. – Barmar Apr 21 '23 at 16:35
  • I store the number in a sql table on the server. There's a whole bunch of functionality built up around this, but it hinges one each tab of each browser getting a different sessionNumber. – George Mastros Apr 21 '23 at 16:37
  • Sorry, misunderstood the question. – Barmar Apr 21 '23 at 16:39
  • This sounds like a bug in Opera, see https://stackoverflow.com/questions/368653/how-to-differ-sessions-in-browser-tabs for what the spec says. – Barmar Apr 21 '23 at 16:41
  • 1
    Note that if a user *duplicates* a tab, the session storage from the original tab is *copied* to the new tab. – Pointy Apr 21 '23 at 16:42

0 Answers0