首页 > webView中页面无法接受到postMessage发出的消息

webView中页面无法接受到postMessage发出的消息

有两个页面:a.html b.html,在a中利用postMessage方法向b发送消息,并设置消息监听事件,代码如下:

<input type="text" name=""><input type="button" name="" value="search">
<p id="name"></p>
<p id="url"></p>
<script type="text/javascript">
    $('input[type="button"]').on('click',function () {
        var mapWindow = window.open('d.html?s=' + $('input[type="text"]').val() + '&nw=1');
        setTimeout(function () {
            mapWindow.postMessage('hello', 'http://192.168.199.191:8080');
        },10000);
    })
    window.addEventListener('message',receiveMessage,false);
    function receiveMessage(e) {
        $('#name').text(e.data.placeName);
        $('#url').text(e.data.imgUrl);
    }
</script>

然后在b页面监听message事件,代码如下:

window.addEventListener('message',receiveMessage,false);
function receiveMessage(e) {
    // if (e.origin ! = ) return;
    $('#log').text(e.data);
    e.source.postMessage(message,e.origin);
}

此代码在浏览器中可以正常运行,在b页面中成功输出信息“hello”,但是将页面放到app里面后,b无法接受到a的消息,经测试a的消息可以发出,a自己可以接受到自己的消息,但是b却没有接受到消息。


你需要设置你的WebView支持multiple windows。

【热门文章】
【热门文章】