2017-04-05 140 views
1

我有2個html文件。如何從iFrame調用Javascript函數?

1.html將從iframe調用2.html。

<iframe src="2.html" > </iframe> 

2.html 定義了2個javascript函數。

function func1(a, b, c) { 
    for() { 
    ..... 
    } 
} 

function func2() { 
func1(a, b, c); 
} 

HTML

<body onload="func2()"> 

現在的問題是,我有這2個HTML文件合併到一個HTML。 2.html必須在iframe中執行。

我試圖做的,

<!DOCTYPE html> 
<html> 

    <body> 
    <iframe id="foo" onload="myFunction()" /> </iframe> 
    <script> 
     function myFunction() { 
     var iframe = document.getElementById('foo'), 
      iframedoc = iframe.contentDocument || iframe.contentWindow.document; 
     iframedoc.body.innerHTML = func2(); 
     } 

    </script> 
    </body> 

</html> 

但FUNC2()沒有被調用。任何人都可以建議在iframe中調用javascript函數的方式嗎?

在此先感謝。

回答