国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > htmlcss > javascript浏览器窗口之间传递数据

javascript浏览器窗口之间传递数据

来源:程序员人生   发布时间:2015-01-18 10:36:19 阅读次数:3691次

摘要:

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

原理:

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

代码:

index.html

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF⑻">
    <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⑻">
    <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>
复制代码

注意:要有服务环境


其他精彩文章

android学习笔记(41)android选项菜单和子菜单(SubMenu )

android学习笔记(40)Notification的功能与用法

android学习笔记(42)android使用监听器来监听菜单事件

android学习笔记(43)android创建单选菜单和复选菜单

android学习笔记(44)android设置与菜单项关联的Activity

android学习笔记(45)android上下文菜单

更多关于android开发文章

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生