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

前端 未结 5 591
小蘑菇
小蘑菇 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 14:05

    window.name can be overwritten by custom javascript libraries, datetimepickers etc.

    Instead of window.name I suggest you to use the DOM head meta tag to store your id

    
    

    After page is loaded, you have to load everything via ajax in that window, then you can attach this ID to every request as a header (or data) value. For example in JQuery with this code:

    $(document)
        .ajaxSend(function(event, jqXHR, ajaxOptions) {
            jqXHR.setRequestHeader('windowID',
                document.getElementById('windowID').content);
    })
    

    To use this solution, you have to have access to custom header values on server side. For example in Java servlet:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String windowName = request.getHeader("windowID");
    

    If you store paging, sorting, filtering etc. information on server side as session attribute, you should store them separately attached to the separate window ID-s.

提交回复
热议问题