浏览器窗口之间传递数据

摘要:

  在项目开发中我们经常会遇到弹窗,有的是通过div模拟弹窗效果,有的是通过iframe,也有通过window自带的open函数打开一个新的窗口。今天给大家分享的是最后一种通过window.open()函数打开页面进行数据交互。首先看下效果图:

原理:

  父窗口给子窗口传递数据是通过url的参数传递过去,子窗口给父窗口传递数据是通过父窗口的全局函数传递。

代码:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="content"></div>
    <button id="test">按钮</button>
    <script>
        var test = document.getElementById('test');
        test.onclick = function() {
            window.open('./window.html?param1=name&param2=password', '_blank','width=960,height=650,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes');
        };
        window.getContent = function(tx) {
            document.getElementById('content').innerText = tx;
        }
    </script>
</body>
</html>

 

window.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="content"></div>
    <select name="" id="city">
        <option value="shanghai">上海</option>
        <option value="hangzhou">杭州</option>
    </select>
    <script>
        var params = location.href.substring(location.href.lastIndexOf('?')+1).split('&');
        document.getElementById('content').innerText = params;
        var city = document.getElementById('city');
        city.onchange = function() {
            window.opener.getContent(city.value);
        }
    </script>
</body>
</html>

注意:要有服务环境

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值