在項目開發中我們常常會遇到彈窗,有的是通過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¶m2=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集成微信支付