在Internet Explorer的新选项卡中打开链接时,SessionStorage不为空 [英] SessionStorage is not empty when link opened in new tab in Internet Explorer

查看:686
本文介绍了在Internet Explorer的新选项卡中打开链接时,SessionStorage不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每个打开的浏览器选项卡上(在javascript对象中)都有一些唯一的ID。必须通过请求保存Id,我决定使用sessionStorage。


当我在浏览器中打开新页面时效果很好。

但是当我点击鼠标右键并在IE 11中选择在新标签中打开链接时 - sessionStorage不为空。所以我对新身份证的期望失败了。

I need to have some unique id on every opened browser tab (in a javascript object). The Id must be saved through requests and i decided to use sessionStorage for it.

When i opens the new page in browser it works well.
But when i click a link by right mouse button and choose 'Open link in new tab' in IE 11 - sessionStorage is not empty. So my expectations about new id failed .

Chrome以另一种方式运作,sessionStorage为空。

Chrome works another way, sessionStorage is empty.

有谁知道如何为IE解决这个问题?

Does anyone know how to solve this problem for IE?

推荐答案

我知道这是一个古老的问题,但我今天只是在努力解决这个问题。我正在打开一个新标签,其中包含 target =_ blank的链接,期望sessionStorage为空。它不是。

I know this is an ancient question but I just today struggled with this myself. I was opening a new tab with a link with target="_blank" expecting the sessionStorage to be empty. It was not.

注意:所有这些都是未经测试的代码,但你应该得到jist。我遇到这个问题的浏览器是Firefox 44.0.2。

NOTE: All of this is untested code but you should get the jist. Also the browser I was experiencing this problem with was Firefox 44.0.2.

我的代码是这样的:

有问题的代码:

$(window).ready(function(){
    //Get saved session data
    var persistedObject = null;

    try{
        persistedObject = JSON.parse(sessionStorage.getItem('tabData'));
    }catch(e){}
});

function updateSessionStorage(){
    var objectToPersist = {};

    //Get some data to persist
    objectToPersist.value1 = "some data";

    //Save the data in the session storage
    sessionStorage.setItem('tabData', JSON.stringify(objectToPersist));
}

这是从之前关闭的标签中获取的数据。

This picked up data from earlier tabs that were closed.

我通过将我的代码更改为以下内容解决了这个问题:

I solved this by changing my code to something like this:

工作代码:

$(window).ready(function(){
    //Get saved session data
    var persistedObject = null;

    try{
        persistedObject = JSON.parse(window.name);
    }catch(e){}
});

function updateSessionStorage(){
    var objectToPersist = {};

    //Get some data to persist
    objectToPersist.value1 = "some data";

    //Save the data in the session storage
    window.name = JSON.stringify(objectToPersist);
}

这很有效。在关闭选项卡/窗口之前,窗口名称将保持不变,这正是我期望sessionStorage执行的操作。如果链接处理方没有给出名字,则新页面总是加载 window.name =

This worked beautifully. The window name is persisted until you close the tab/window, which is exactly what I was expecting sessionStorage to do. New pages is always loaded with window.name = "" if no name was given on the link handling side.

你可能错过的是你不能直接开箱即用的名字,比如我的 sessionStorage.setItem('tabData','some data')但你可以通过这样的方式轻松避免这种情况:

What you might miss out on is the fact that you can't straight out of the box use names such as my sessionStorage.setItem('tabData', 'some data') but you can easily avoid this by doing something like this:

建议:

function updateSessionStorage(){
    var objectToPersist = {};

    try{
        objectToPersist = JSON.parse(window.name);
    }catch(e){}

    if(objectToPersist[tabData] == undefined){
        objectToPersist.tabData = {};
    }

    //Get some data to persist
    objectToPersist.tabData.value1 = "some data";

    //Save the data in the session storage
    window.name = JSON.stringify(objectToPersist);
}

我希望有人觉得这很有用。

I hope someone finds this useful.

这篇关于在Internet Explorer的新选项卡中打开链接时,SessionStorage不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆