[英]Calling javascript function in iframe
我在從父頁面調用iframe
的 JavaScript 函數時遇到問題。 這是我的兩個頁面:
主頁.html
<html>
<head>
<title>MainPage</title>
<script type="text/javascript">
function Reset()
{
if (document.all.resultFrame)
alert("resultFrame found");
else
alert("resultFrame NOT found");
if (typeof (document.all.resultFrame.Reset) == "function")
document.all.resultFrame.Reset();
else
alert("resultFrame.Reset NOT found");
}
</script>
</head>
<body>
MainPage<br>
<input type="button" onclick="Reset()" value="Reset"><br><br>
<iframe height="100" id="resultFrame" src="resultFrame.html"></iframe>
</body>
</html>
結果框架.html
<html>
<head>
<title>ResultPage</title>
<script type="text/javascript">
function Reset()
{
alert("reset (in resultframe)");
}
</script>
</head>
<body>
ResultPage
</body>
</html>
(我知道不推薦使用document.all
但此頁面只能在內部使用 IE 查看,我認為這不是問題)
當我按下重置按鈕時,我得到“resultFrame found”和“resultFrame.Reset NOT found”。 好像有frame的引用,但是不能調用frame上的函數,這是為什么呢?
用:
document.getElementById("resultFrame").contentWindow.Reset();
訪問 iframe 中的重置功能
document.getElementById("resultFrame")
將在您的代碼中獲取 iframe,而contentWindow
將在 iframe 中獲取窗口對象。 一旦有了子窗口,就可以在該上下文中引用 javascript。
另請參閱此處,特別是 bobince 的回答。
為了更加穩健:
function getIframeWindow(iframe_object) {
var doc;
if (iframe_object.contentWindow) {
return iframe_object.contentWindow;
}
if (iframe_object.window) {
return iframe_object.window;
}
if (!doc && iframe_object.contentDocument) {
doc = iframe_object.contentDocument;
}
if (!doc && iframe_object.document) {
doc = iframe_object.document;
}
if (doc && doc.defaultView) {
return doc.defaultView;
}
if (doc && doc.parentWindow) {
return doc.parentWindow;
}
return undefined;
}
和
...
var el = document.getElementById('targetFrame');
var frame_win = getIframeWindow(el);
if (frame_win) {
frame_win.reset();
...
}
...
不要從文檔中獲取框架,而是嘗試從窗口對象中獲取框架。
在上面的例子中改變這個:
if (typeof (document.all.resultFrame.Reset) == "function")
document.all.resultFrame.Reset();
else
alert("resultFrame.Reset NOT found");
到
if (typeof (window.frames[0].Reset) == "function")
window.frames[0].Reset();
else
alert("resultFrame.Reset NOT found");
問題是 iframe 內 javascript 的范圍沒有通過 iframe 的 DOM 元素公開。 只有窗口對象包含框架的 javascript 范圍信息。
稱呼
window.frames['resultFrame'].Reset();
objectframe.contentWindow.Reset()
您首先需要引用框架中的頂級元素。
需要滿足的首要條件是父級和 iframe 應該屬於同一個源。 完成后,子進程可以使用 window.opener 方法調用父進程,父進程也可以對子進程執行上述操作
當您通過 document.all 訪問 resultFrame 時,它只會將其作為 HTML 元素而不是窗口框架拉取。 如果框架使用“this”自引用觸發事件,則會遇到同樣的問題。
代替:
document.all.resultFrame.Reset();
和:
window.frames.resultFrame.Reset();
或者:
document.all.resultFrame.contentWindow.Reset();
如果您無法直接使用它並且遇到此錯誤:阻止了一個帶有“ http://www ..com”源的框架訪問跨源框架。 您可以使用postMessage()而不是直接使用該函數。
<iframe>
調用尊重隱私的XMLHttpRequest
HTML:
<iframe id="iframe"></iframe>
JavaScript:
const iframe = document.getElementById('iframe');
const iframeWin = iframe.contentWindow || iframe;
const iframeDoc = iframe.contentDocument || iframeWin.document;
let script = iframeDoc.createElement('SCRIPT');
script.append(`function sendWithoutOrigin(url) {
let request = new XMLHttpRequest();
request.open('GET', url);
request.onreadystatechange = function() {
if(request.readyState === XMLHttpRequest.DONE) {
if(request.status === 200) {
console.log('GET succeeded.');
}
else {
console.warn('GET failed.');
}
}
}
request.send();
}`);
iframeDoc.documentElement.appendChild(script);
JavaScript 喚起:
let url = 'https://api.serivce.net/';
url += '?api_key=' + api_write_key;
url += '&field1=' + value;
iframeWin.sendWithoutOrigin(url);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.