第一,我知道這個問題與其他問題重複。但我試圖做以下很多來自stackoverflow中的許多問題的示例,但它沒有奏效。我無法從iframe調用主窗口的功能,但是如果我使用框架標記,它是可行的。如何從iframe調用主窗口中的函數?
我試過這樣。
主窗口:
<body>
<iframe src='test.html'></iframe>
<script>
var alertFunc = function(){
window.alert("work!");
}
</script>
</body>
的test.html從控制檯
<body>
<input type="button" onclick="window.parent.alertFunc();" value="Test" />
</body>
錯誤消息。
Unsafe JavaScript attempt to access frame with URL file:///C:/.../index.html from frame with URL file:///C:/.../test.html. Domains, protocols and ports must match.
test.html:30
Uncaught TypeError: Cannot call method 'alertFunc' of undefined
和許多形式類似。
你有什麼想法嗎?或者我做錯了什麼?
試試這個: http://stackoverflow.com/questions/2161388/iframe-function-calling-from-iframe- to-parent-page-javascript-function – blackpg 2013-03-15 02:43:42
檢查您的瀏覽器控制檯是否有任何錯誤 – 2013-03-15 02:51:17
@Arun P Johny其錯誤更新爲 – fronthem 2013-03-15 02:59:24