如何實現瀏覽器內多個標簽頁之間的通信?


調用 localstorge、cookies 等本地存儲方式

 

方法一:使用localStorage

使用localStorage.setItem(key,value);添加內容

使用storage事件監聽添加、修改、刪除的動作   

 

  window.addEventListener("storage",function(event){  

          $("#name").val(event.key+”=”+event.newValue);  

  }); 

 

方法二、使用cookie+setInterval

    <inputidinputid="name"><input type="button" id="btnOK"value="發送">  

 

    

$(function(){  

     $("#btnOK").click(function(){  

           varname=$("#name").val();  

           document.cookie="name="+name;  

       });  

  });  

 

//獲取Cookie天的內容  

function getKey(key) {  

    return JSON.parse("{\""+ document.cookie.replace(/;\s+/gim,"\",\"").replace(/=/gim, "\":\"") +"\"}")[key];  

}

 //每隔1秒獲取Cookie的內容  

setInterval(function(){  

    console.log(getKey("name"));  

 },1000);  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM