我正在使用html5的localStorage API。localStorage无法传输数据,不适用于所有浏览器
这种情况是,用户点击一个按钮“添加到我的列表”,以便她可以保存到列表中的位置名称。
以下代码仅适用于Chrome。适用于一个窗口,并且如果两个或多个窗口从同一个源打开。 如果只有一个窗口在Firefox和IE中打开,则可以正常工作。如果有多个窗口,在IE中不会获取数据。在Firefox中,如果点击添加名称,则添加约30次。
我试图根据我找到的例子来实现我的代码。
我该如何解决这个问题?
在此先感谢。
<input type="button" value="Add to my list" onClick="savepoi(document.getElementById('name').innerHTML);"/>
<script>
//listen if a storage happened to another window and call the right function
if (window.attachEvent) { // IE8 support
window.attachEvent('onstorage', storageEvent);}
else
{window.addEventListener('storage', storageEvent, true);}
function storageEvent(event) {
//if something is added
if(event.oldValue==null)
{savepoib(event.newValue);}
}
function savepoi(a){
//if no localStorage, set firs key=1, else add 1 to the existing
//save key and name together
if(localStorage.length==0)
{localStorage.setItem(1,1+"-"+a);}
else
{localStorage.setItem((localStorage.length+1),(localStorage.length+1)+"-"+a);}
}
function savepoib(a){
//split to get key and name
var b =a.split("-");
var idi=b[0];//key
var myname = b[1];//name
if(localStorage.length==0)
{localStorage.setItem(1,1+"-"+myname);}
else
{localStorage.setItem(idi,idi+"-"+myname);}
}
</script>
哦,是的,我记得,大多数IE实现,我试图解雇存储事件,但事件是缺乏信息,没有newValue或oldValue或任何东西。但是这是IE10之前我正在做这个测试。事情可能已经改变。 –