postMessage h5

index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>页面一</title>
</head>
<body>
    <button onclick="sendPage2();" type="button">send page2</button>
    <hr>
    <iframe src="http://192.168.3.24/jyn/admins/wx/add_material" name="iframe" frameborder="0" scrolling="no"></iframe>
</body>
<script>
    function sendPage2(){
        window.frames.iframe.postMessage("this is page one","*");
    }
    
    window.addEventListener("message", function( e ) {
        console.log(e.data);
    }, false );
    
</script>
</html>

index2.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>页面二</title>
    <style>
    * {
        margin: 0;
        padding: 0;
    }
    .box {
        width: 400px;
        height: 200px;
        background: #4E98DD
    }
    </style>
</head>
<body>
    
    
<div class="box">
    this is node page
    <button id="btn" onclick="sendParent()" type="button">send parent</button>
</div>

<script>
    window.addEventListener("message", function( e ) {
        console.log(e.data);
    }, false );
    
    function sendParent(){
        window.parent.postMessage("this pages2","*")
    }
</script>

</body>
</html>

猜你喜欢

转载自my.oschina.net/u/3449950/blog/1932306