javascript瀏覽器視窗之間傳遞資料,瀏覽器javascript

來源:互聯網
上載者:User

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&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>

注意:要有服務環境


其他精彩文章

android學習筆記(41)android選項菜單和子功能表(SubMenu )android學習筆記(40)Notification的功能與用法android學習筆記(42)android使用監聽器來監聽菜單事件android學習筆記(43)android建立單選菜單和複選菜單android學習筆記(44)android設定與功能表項目關聯的Activityandroid學習筆記(45)android操作功能表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.