在h5中,新增了localStorage,对应localStorage的有一个storage事件,这个事件可以用来进行页面间进行通信。
页面间通信的列子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>A</title>
</head>
<body>
<button>click2</button>
</body>
<script>
document.querySelector('button').onclick = function(){
localStorage.setItem('Num', Math.random()*10);
}
window.addEventListener("storage", function (e) {
console.log(e)
console.log(e.newValue)
alert('来自其他页面的localstorage');
});
var orignalSetItem = localStorage.setItem;
localStorage.setItem = function(key,newValue){
var setItemEvent = new Event("setItemEvent");
setItemEvent.newValue = newValue;
window.dispatchEvent(setItemEvent);
alert('来自本页面的localstorage');