监听H5本地存储(同源跨页面通讯)

var addEvent = (function(){
    if (document.addEventListener){
        return function(el, type, fn){
            if (el.length) {
                for(var i = 0; i < el.length; i++){
                    addEvent(el[i], type, fn);
                }
            }else{
                el.addEventListener(type, fn, false);
            }
        };
    }else{
        return function(el, type, fn){
            if(el.length){
                for(var i = 0; i < el.length; i++){
                    addEvent(el[i], type, fn);
                }
            }else{
                el.attachEvent('on' + type,
                function() {
                    return fn.call(el, window.event);
                });
            }
        };
    }
})();
addEvent(window,'storage',function(event){
  if(event.key == 'storage_data'){
      console.log("key:"+event.key+" ---- old:"+event.oldValue+" ---- new:"+ event.newValue);
  }
});

 

你可能感兴趣的:(Javascript,HTML5)