如何检测post.message中的null var?

时间:2018-07-30 00:11:27

标签: javascript jquery

你好,我需要检测child.php是否不会将消息发送到parent.php,我使用post.message。 我尝试:

if (message == null), == false, == "", but not work ..

我的代码:

parent.php(如果打开则从child.php获取消息“工作”)

<button id="open-child-window-2">Open Child Window</button>

<script>

var __CHILD_WINDOW_HANDLE_2 = null;

$("#open-child-window-2").on('click', function() {
    __CHILD_WINDOW_HANDLE_2 = window.open('child-2.php', '_blank');
});


window.addEventListener('message', function(e) {
    ProcessChildMessage_2(e.data);
} , false);


function ProcessChildMessage_2(message) {
    alert(message);
}

</script>

child.php(每0.5秒向“ parent.php”发送“工作”消息)

<script>

setInterval(function(){

    window.opener.postMessage("work", "*");

},500);

</script>

我需要检测chiel.php是否不会将“工作”发送给parent.php,例如:

  1. 如果关闭child.php
  2. 如果从child.php转到另一页

非常感谢您

1 个答案:

答案 0 :(得分:0)

那你为什么不使用[window.onbeforeunload](https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload)(昨天我的声誉是53,今天又下降到49,所以我无法发表评论。)