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?
has memory leakage too. The effect is more renounced in IE browsers.
You can change the src of the iframe using two methods:
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.
I get this from somewhere else :
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!