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

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


you could set your own window name, the exact syntax escapes me right now, but you can use the current time and session id to create a unique id on window load, then use that id

This would be done the same way you set a name in the javascript window.open() function, (but you can do it to self, instead of new window)

googling shows:

self.window.name = myclass.getUniqueWindowId( thisSession );

UPDATE

Regarding your need to save this from refresh to refresh, i did some tests and it looks to save it from refresh to refresh. Using Firefox 3, on initial load, the window name is blank, and pressing CTRL+R over and over, and the window name was populated. i then commented out the setting the name code and reloaded and it still retained the name.

<script type="text/javascript">    alert( self.window.name );    self.window.name = "blah";</script>

UPDATE

I have to make noticed the comment below on jQuery's 'jquery-session' plugin, which really works and offers way more than what's discussed here.

Although, one should also make it clear that it relies on HTML5's Web Storage, not supported by older IE versions.

Corporate still depends heavily on IE 7 ('and below' here in Brazil).

Based on self.window.name, THE solution for everything non-compliant to HTML5, I offer the following code snippet as a cross-browser solution:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script><script language="javascript" type="text/jscript">    //----------------------------------------------------------------------    //-- guarantees that window.name is a GUID, and that it would    //-- be preserved whilst this window's life cicle    //----------------------------------------------------------------------    //-- window.name will be set to "GUID-<SOME_RANDOM_GUID>"    //----------------------------------------------------------------------    $(window).load(        function () {            //----------------------            var GUID = function () {                //------------------                var S4 = function () {                    return(                            Math.floor(                                    Math.random() * 0x10000 /* 65536 */                                ).toString(16)                        );                };                //------------------                return (                        S4() + S4() + "-" +                        S4() + "-" +                        S4() + "-" +                        S4() + "-" +                        S4() + S4() + S4()                    );            };            //----------------------            if (!window.name.match(/^GUID-/)) {                window.name = "GUID-" + GUID();            }        }    ) //--------------------------------------------------------------------</script>

I found the GUID function here (for which I proposed some code clean-up).


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.


What about having your server randomly generate an ID and have that stored in the page (some javascript variable) when it's served? Then just include that ID in the ajax request. It wont' help on a browser refresh, but as long as the user leaves that page in place (and just lets the ajax stuff do its thing) it should work fine.