父頁面要有此函數
//子頁面回調此函數
IndexCallback = function() {
console.log('我是父頁面的方法')
}
子頁面回調父頁面的方法
window.parent.IndexCallback();
延伸
iframe子父頁面間js的互相調用
1、iframe子頁面調用父頁面js函數
子頁面調用父頁面函數隻需要寫上window.praent就可以了。比如調用a()函數,就寫成:
window.parent.a();
子頁面取父頁面中的标簽中的值,比如該标簽的id為“test”,則:
window.parent.document.getElementById("test").value;
jQuery方法為:
$(window.parent.document).contents().find("test").val();
但是我在chrome浏覽器下卻發現此方法無效了!查了半天才了解,在chrome 5+中,window.parent無法在file://協定中運作,但是釋出了之後http://協定下是可以運作的。此方法支援ie、firefox浏覽器。
2、iframe父頁面調用子頁面js函數
這個就稍微複雜一些,下面的方法支援ie和firefox浏覽器:
document.getElementById('ifrtest').contentWindow.b();
子頁面取父頁面中的标簽中的值,比如該标簽的id為“test”,則:
document.getElementById("test").value;
注:ifrtest是iframe架構的id,b()為子頁面js函數。contentWindow屬性是指定的frame或者iframe所在的window對象,IE下可以省略。