有兩個(gè)頁面:a.html b.html,在a中利用postMessage方法向b發(fā)送消息,并設(shè)置消息監(jiān)聽事件,代碼如下:<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頁面監(jiān)聽message事件,代碼如下:window.addEventListener('message',receiveMessage,false);function receiveMessage(e) { // if (e.origin ! = ) return;
$('#log').text(e.data);
e.source.postMessage(message,e.origin);
}此代碼在瀏覽器中可以正常運(yùn)行,在b頁面中成功輸出信息“hello”,但是將頁面放到app里面后,b無法接受到a的消息,經(jīng)測試a的消息可以發(fā)出,a自己可以接受到自己的消息,但是b卻沒有接受到消息。
webView中頁面無法接受到postMessage發(fā)出的消息
繁星coding
2018-08-10 08:09:05