从父HTML调用函数

时间:2016-10-14 17:15:40

标签: javascript jquery html

在HTML中我有两个函数,一个是abc,只有一个警报,另一个是创建一个新窗口,window.open()填充预定义的HTML字符串。我想从父HTML调用该函数。我试过调用parent.abc和window.parent.abc。它们都不起作用。

<html>
<body>
<button onclick="myfunction()" type="button">A new Window</button> 
<script>
    var myWindow; 
    function abc() {
        alert("abc");
    };

    function myfunction() {
        var htmlholder = '<!DOCTYPE html>'+
                     '<html>'+
                     '<body>'+
                     '<p>Here is another HTML<\/p>'+
                '<button type="button1" onclick="parent.abc()">Call parentabc<\/button>'+
                     '<\/body>'+
                     '<\/html>';

        myWindow = window.open("", "MsgWindow", "width=200,height=100");
        myWindow.document.write(htmlholder);

    };

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

你必须使用开启者而不是父母。添加了小提琴手。

   function myfunction() {
        var htmlholder = '<!DOCTYPE html>'+
                     '<html>'+
                     '<body>'+
                     '<p>Here is another HTML<\/p>'+
                '<button type="button1" onclick="opener.abc()">Call parentabc<\/button>'+
                     '<\/body>'+
                     '<\/html>';

        myWindow = window.open("", "MsgWindow", "width=200,height=100");
        myWindow.document.write(htmlholder);

    };
  

https://jsfiddle.net/0txe2qvx/