没有从iframe收到postMessage

时间:2015-11-10 12:53:50

标签: javascript iframe postmessage

以下是iframe中public void Execute(object parameter) { _mainViewModel.SaveSomething(); var editWindow = parameter as MyEditWindow; editWindow?.Close(); }

的代码
<Button Content"Save&Close" Command="{Binding SaveCmd}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"  IsDefault="True" />

以下是src="example.com"中运行的代码,其中包含postMessage接收器(还包含iframe):

<script>

    var domain = "http://example2.com";

    function redirectRequest(){

        console.log("window.opener",window.opener); // NULL
        console.log("window.top",window.top); // script_name
        console.log("window.parent",window.parent); // script_name

        opener.postMessage("redirect", domain); //fails because null
       //top and parent also do not work BUT do not display errors
    }

</script>

知道可能会发生什么吗?非常感谢你......

2 个答案:

答案 0 :(得分:2)

对于iframe,您需要将opener替换为parent。 替换这个:

opener.postMessage("redirect", domain);

到此:

window.parent.postMessage("redirect", domain);

答案 1 :(得分:0)

就我而言,出于某种原因,这有帮助:

// Inside iframe
window.parent.socket.postMessage('msg example', '*');