In javascript, how can I uniquely identify one browser window from another which are under the same cookiedbased sessionId

前端 未结 5 598
小蘑菇
小蘑菇 2020-12-02 13:20

The users of my web application may have more than one browser window open and pointed to the same page. I would like the state of certain things in the page (loaded via aj

5条回答
  •  再見小時候
    2020-12-02 13:59

    You can use HTML5 session Storage ,just generate an unique id and set it on the session storage ! what is cool about that each window or tab has its own session storage. for example :

    if we run the following on 3 windows:

    window 1: sessionStorage.setItem('key' , 'window1');

    window 2: sessionStorage.setItem('key' , 'window2');

    window 3: sessionStorage.setItem('key' , 'window3');

    sessionStorage.getItem('key' ); <<< this will return corresponding value on window!

    window 1: sessionStorage.getItem('key' ); returns window 1

    window 2: sessionStorage.getItem('key' ); returns window 2

    window 3: sessionStorage.getItem('key'); returns window 3

    I believe you are trying to save a variable (separately on each tab/window).

    sessionStorage works as charm.

    The only problem you may face that browser should support HTML 5.

提交回复
热议问题