2015-07-21 49 views
0

我在頁面上有一個iframe元素,我需要在iframe中創建一些Object對象的原型方法。在IE8中訪問iframe Object.prototype

我的iframe窗口

var win = iframe.contentWindow || iframe.contentDocument.defaultView; 

然後一個試圖建立的方法。在FF,Chrome和IE9 +這很簡單:

win.Object.prototype.func = myFunc; 

但它在IE8中不起作用。我在win.Object中未定義。 同時win.Element完美運作。

如何獲得IE8中的iframe對象?

回答

0

好吧,這很容易。 足以在iframe中創建空腳本 - 並且對象(如win.Object,win.Element等)在IE中可用。

1

您可以在其中創建的外窗的全局變量指向內Objectiframe插入script元素:

var doc = iframe.contentDocument, 
    s = doc.createElement('script'); 
s.text = "parent.Object2 = Object;"; 
doc.appendChild(s); 
doc.removeChild(s); 

然後,您可以使用Object2.prototype