87,899
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="点击"/>
<script>
document.querySelector("input").onclick=function(){
if(!localStorage.getItem("open")){
window.open("./demo1.html");
}
localStorage.setItem("text","点击")
}
</script>
</body>
</html>
B页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="box"></div>
<script>
window.onunload=function(){
localStorage.removeItem("open")
}
window.onload=function(){
localStorage.setItem("open","111");
setInterval(function(){
if(localStorage.getItem("text")){
document.querySelector("#box").innerText+=localStorage.getItem("text");
localStorage.removeItem("text")
}
},100)
}
</script>
</body>
</html>
这里用localStorage而不用sessionStorage的原因是 window.open打开的窗口session不能同步
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="点击"/>
<script>
var newWindow=null;
document.querySelector("input").onclick=function(){
if(!newWindow){
newWindow=window.open("./demo1.html");
newWindow.onload=function(){
newWindow.document.querySelector("#box").innerText+="点击"
}
}else {
newWindow.document.querySelector("#box").innerText+="点击"
}
}
</script>
</body>
</html>
子窗口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="box"></div>
<script>
window.onunload=function(){
window.opener.newWindow=null;
}
</script>
</body>
</html>
这样是可以实现,但是不管哪个页面刷新就会失效了