2015-11-05 83 views
1

我有一些代碼正在調用一個存在於window.external上的函數的html頁面中運行。代碼工作:通過javascript設置iframe的window.external成員

SomeCode.js(生活在somePage.html):

window.external['someFunction'](); 

然而,出於測試目的,我需要嵌入在一個單獨的測試頁的iframe內的HTML頁面。測試頁代碼

TestPage.html:

window.frames[0].external['someFunction'] = function() { console.log('success'!); }; // Doesn't work 
window.frames[0].src = "http://somePage.com/somePage.html"; 
window.frames[0].external['someFunction'] = function() { console.log('success'!); }; // window.external['someFunction'] is still null from the perspective of the code in SomeCode.js. oesn't work 

我已經嘗試了一些前後都設置SRC後設置功能的方法。奇怪的是,如果我打印window.frames [0] .external到控制檯,window.frames [0] .external DOES似乎有'someFunction'設置。它幾乎就像window.frames [0] .external是與iframe中javascript內部使用的對象不同的對象。

任何想法將不勝感激。

感謝, 蟬

回答

0

如果函數someFuction有一些PARAMATERS,您必須在先例行宣佈其。如果你不這樣做,它不起作用。

應該是:

window.frames[0].external['someFunction'] = function(params) {console.log('success!');};