本文介绍了在JavaScript中,我怎么能独特地从其他标识一个浏览器窗口,也按相同的cookiedbased的sessionId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序的用户可能有多个浏览器窗口打开并指出在同一页。我想的东西在页面的某些状态(通过AJAX加载)在回传被保留。我可以存储在cookie中或我的服务器上。无论哪种方式,我想不出我怎么能区分每个窗口。

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 in things in the page (loaded via ajax) to be retained across postbacks. I can either store in a cookie or on my server. Either way, I can't think of how I can distinguish each window.

举例来说,假设用户Bob有两个浏览器窗口打开到ListOfSomething页面。每个列表都有的 LoadedPageNumber 的属性,我需要坚持。否则,用户永远结束第1页上时,他们刷新。鲍勃可能加载浏览器窗口1,并指出其第5页,然后加载浏览器窗口,2并指出其第14页。如果我只是存储基于会话ID的属性,鲍勃将获得14页面窗口1,如果他刷新它。

For example, say user Bob has two browser windows open to the ListOfSomething page. Each list has a LoadedPageNumber attribute which I need to persist. Otherwise users always end up on page 1 when they refresh. Bob might have loaded browser window 1 and pointed it to page 5 and then loaded browser window 2 and pointed it to page 14. If I just store the attribute based on session id, Bob will get page 14 in window 1 if he refreshes it.

请注意,我的状态变量实际上比这个简单的例子,我无法坚持他们可能会导致大问题要复杂得多(在我的应用程序的弱点)。

Note that my state variables are actually much more complex than this simple example and my inability to persist them could lead to big problems (weaknesses in my app).

我需要某种形式的浏览器窗口ID或东西。这当然需要一个跨浏览器的解决方案(IE6 +,Wekbit +,FF2 +)

I need some kind of browser window id or something. It of course needs to be a cross-browser solution (IE6+, Wekbit?+, FF2+)

任何想法?

感谢

请注意上的相关性:请记住,这是也有用在那里你混合旧的形式根据较新的支持AJAX项目的页面的情况。有时你需要回传的形式,你不想失去一些客户端的状态值。

Note on relevance: Keep in mind that this is useful also for the case where you're mixing older forms based pages with newer AJAX enabled items. Sometimes you need to postback the forms and you don't want to loose some client side state values.

推荐答案

您可以设置自己的窗口名,确切的语法脱离了我的权利,但你可以使用当前的时间和会话ID创建窗口一个唯一的ID负载,然后使用该ID

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

您在使用JavaScript的window.open设置一个名称(这将做同样的方式)的功能,(但你可以把它做的,而不是新的窗口,自我,)

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)

谷歌搜索显示:

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

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

关于你需要保存这个从刷新,刷新,我做了一些测试,它看起来保存它从刷新刷新。使用Firefox 3,初始负载,窗口名称为空白,pressing CTRL + R了个遍,但窗口名被填充。然后我注释掉设置名称$​​ C $ c和重新加载,它仍然保留了名字。

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>

更新

我一定要注意到下面的评论对jQuery的'jQuery的会话插件,它真正的作品,并提供比在这里讨论的方式了。

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.

虽然,还应该清楚地表明,它依赖于HTML5的Web存储,不是旧的IE浏览器版本的支持。

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

企业仍然在很大程度上依赖于IE 7(以下在巴西)。

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

根据 self.window.name ,一切解决方案不符合HTML5,我提供以下code段作为一个跨浏览器的解决方案:

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>

我找到了GUID功能这里(对于这我提出了code清理)。

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

这篇关于在JavaScript中,我怎么能独特地从其他标识一个浏览器窗口,也按相同的cookiedbased的sessionId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:39