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

來源:互聯網
上載者:User

javascript瀏覽器視窗之間傳遞資料的方法,瀏覽器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>

注意:這裡需要有服務環境運行

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.