[英]jQuery: How to manipulate the contents of an iframe when the src is a different domain?
[英]Can one set an iframe's src and manipulate its contents afterwards?
我的目標是讓父頁面將iframe的src從空白更改為適當的url(以在給定的位置利用iframe中的onload處理程序,但這是緊要關頭),然后操縱iframe的內容。 但是,javascript似乎會忽略DOM加載時iframe不在其src上的任何元素。 有沒有辦法解決?
setTimeouts旨在允許DOM和iframe加載。 編輯:修復了一些東西。 這是包含頁面:
<html><head>
<script type="text/javascript">
var done = false;
var theIframe;
window.onload = function () {
setTimeout('stuff()', 2000);
clearTimeout('stuff()');
}
function stuff() {
if (!done) {
theIframe = window.myiframe;
theIframe.src = 'http://localhost/TestStuff/redirectIframe.jsp';
done = true;
stuff();
} else {
theIframe.setMe = true;
}
}
</script>
</head>
<body>
<iframe src="" width="500" height="500" id="myiframe" name="myiframe">
</iframe>
</body>
這是iframe:
<html>
<head>
<script type="text/javascript">
var setMe = false;
window.onload = setInterval('checker()', 1000);
function checker() {
alert('hi');
if (setMe) {
window.onload = null;
top.location = 'http://www.google.com';
alert('foundit');
} else alert('a');
}
</script>
</head>
<body></body>
</html>
有任何想法嗎?
在這段代碼中:
theIframe.src = ...;
stuff();
您正在調用stuff()
,這與您所描述的相反,因此實際上您不允許任何時間加載頁面。 也許您對setTimeout
工作方式感到困惑:它只是在指定的時間之后安排一次執行,它不會自動延遲對函數的所有調用。
另外,您只能將clearTimeout
與setTimeout
返回的先前ID一起使用,而不能與現在的代碼一起使用。
嘗試這樣的事情:
window.onload = function() {
loadFrame();
}
function loadFrame() {
theIframe = ...;
theIframe.src = ...;
setTimeout(setSomething, 2000);
}
function setSomething() {
theIframe.setMe = true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.