javascript瀏覽器視窗之間傳遞資料,瀏覽器javascript
摘要: 在項目開發中我們經常會遇到彈窗,有的是通過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¶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-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>注意:要有服務環境
其他精彩文章
android學習筆記(41)android選項菜單和子功能表(SubMenu )android學習筆記(40)Notification的功能與用法android學習筆記(42)android使用監聽器來監聽菜單事件android學習筆記(43)android建立單選菜單和複選菜單android學習筆記(44)android設定與功能表項目關聯的Activityandroid學習筆記(45)android操作功能表