可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have used an iframe which looks like this:
<iframe style='width: 330px; height: 278px' scrolling='no' name="iframeId" class="advPlayer" id="iframeId" frameborder="0" src='../../player/iabpreview.php?adid=<?php echo $selectedAdIdx ?>&autoPlay=true'></iframe>
Whenever I click on a <div>
, I have to change the source of the iframe. I am using the following code:
if ($j.browser.msie) {
frames['iframeId'].window.location="../player/iabpreview.php?adid="+adId+"&autoPlay=true";
}else {
$j(".advPlayer").eq(0).attr("src", "../player/iabpreview.php?adid="+adId+"&autoPlay=true");
}
This works with Firefox, but not with Internet Explorer.
What code would work for Internet Explorer too?
回答1:
You should never user 'browser detection', but feature detection. The most safe way imho is this:
function SetIFrameSource(cid, url){
var myframe = document.getElementById(cid);
if(myframe !== null){
if(myframe.src){
myframe.src = url; }
else if(myframe.contentWindow !== null && myframe.contentWindow.location !== null){
myframe.contentWindow.location = url; }
else{
myframe.setAttribute('src', url);
}
}
}
Just test if the src property is available. If not, test on content window, and the last try is setAttribute.
回答2:
document.getElementById("iframeId").src = "Your URL here."
回答3:
I've tried getElementById
and a lot of other variants. None worked the same on IE, FF, Opera, or Chrome.
This one works well: parent.frames.IFRAME_ID.location.href = '/';
回答4:
The frames collection returns window objects (or the equivalent of). You want to target the document object; try doing:
window.frames['iframeId'].document.location.href = ....
This works in IE, FF, Safari, and so on, so no need for the messy browser detection too.
nb. IIRC the frames collection references name in IE, id in other browsers, so you need both name and id attribute on the - but you already have that, so no worries!
回答5:
Since you've used the jquery tag this might be handy.
function resizeIframe(height) {
height = parseInt(height);
height += 100;
$('iframe#youriframeID').attr('height', height);
}
In case you are doing cross-browser resizing have a look at this post which explains how to automatically resize the height based on the iframes content. You will need access to the html of the iframed website.
Resizing an iframe based on content
回答6:
var myFrame = document.getElementById('myFrame');
myFrame.src = 'http://example.com';
It has memory leakage. After the iframe src has changed many times, your browser slows down to a crawl.
回答7:
window.frames['iframeId'].document.location.href =...
has memory leakage too. The effect is more renounced in IE browsers.
回答8:
document.getElementById('MsgBoxWindow').getAttribute('src')
works in Internet Explorer, Firefox, Safari, to get the source URL.
document.getElementById('MsgBoxWindow').setAttribute('src', urlwithinthedomain)
works on the same browsers to set the source URL.
However, the iframe must be loaded before calling. Don't place it before the iframe while calling the JavaScript code. You can place both after the iframe, if you are calling it right after the page load, and it will work as expected.
回答9:
I get this from somewhere else :
function getElementById(strId) {
var element;
if (document.getElementById) {
element = document.getElementById(strId);
}
else if (document.all) {
element = document.all[strId];
} else if (document.layers) {
element = document.layers[strId];
} else {
element = eval("document." + strId);
}
return element;
}
回答10:
<script type="text/javascript">
function changesrc(iframid, pagesrc)
{
document.getElementById(iframid).src = pagesrc;
}
</script>
<table style="width: 100%">
<tr>
<td>
<a href="#" onclick="changesrc('iframe1','page1.php')">Page1</a> <br/>
<a href="#" onclick="changesrc('iframe1','page2.php')">Page2</a> <br/>
<a href="#" onclick="changesrc('iframe1','page3.php')">Page3</a>
</td>
<td>
<iframe id="iframe1" src="page1.php" scrolling="no"
height="100%" width="100%" style="border:0px;"></iframe>
</td>
</tr>
</table>
回答11:
You can change the src of the iframe using two methods:
- Changing the href.
- Changing the src.
Both methods require that the iFrame be completely loaded before you attempt the modification.
Changing the href works in all browsers, including IE5.
You can address the frame
By refering to the contentwindow of the element:
var myFrame = document.getElementById('myFrame');
myFrames.contentWindow.location = 'http://example.com';
By refering to the NAME of the element:
var myFrame = window.frames.myFrame; // where myFrame is the NAME of the element
myFrame.location = 'http://example.com';
Changing the src is as said above, by selecting the element and changing the src - but it must be a child, not a descendant element.
var myFrame = document.getElementById('myFrame');
myFrame.src = 'http://example.com'
回答12:
Just use the following code.
var iframe = document.getElementById('IFRAME ID'); // just for clarity
iframe .src = 'URL here';
It should work with every browser.
回答13:
Note that if you are only changing the #anchor of the URL (i.e. changing from page.php#this to page.php#that) then IE won't refresh the page but Chrome and Firefox will. If this is your issue then you may want to add something random to your query string to make IE think it's a totally new page and force the reload.
For example (using jQuery):
var hash = 'yourHash';
var rand = 1 + Math.floor(Math.random() * 9999);
var helpUrl = 'page.php?rand=' + rand + '#' + hash;
$('#iframe').attr('src', helpUrl);