2016-10-14 43 views
0

HTMLには2つの機能があり、1つはアラートのみ、もう1つは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> 
+3

ポップアップ/タブ 'opener'、フレームは' parent'を持っています。 – Teemu

+0

ありがとうございます。 – user3707157

答えて

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/

+0

それは動作します。ありがとうございました。 :) – user3707157

+0

@ user3707157それから答えをマーク:) –

関連する問題