rohini
rohini

Reputation:

change iframe source in ie using javascript

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 ?>&amp;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?

Upvotes: 15

Views: 62416

Answers (13)

Andi T
Andi T

Reputation: 616

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 = '/';

Upvotes: 2

Andrew
Andrew

Reputation: 3381

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);

Upvotes: 0

Nasirali
Nasirali

Reputation: 379

<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>

Upvotes: 1

Corniel
Corniel

Reputation: 251

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.

Upvotes: 25

giorgi
giorgi

Reputation:

Just use the following code.

var iframe = document.getElementById('IFRAME ID'); // just for clarity
iframe .src = 'URL here';

It should work with every browser.

Upvotes: 0

vince
vince

Reputation: 11

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;
}

Upvotes: 1

EHCanadian83
EHCanadian83

Reputation: 11

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.

Upvotes: 1

kli
kli

Reputation: 11

window.frames['iframeId'].document.location.href =...

has memory leakage too. The effect is more renounced in IE browsers.

Upvotes: 1

kl2217
kl2217

Reputation: 11

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.

Upvotes: 1

Kirtan
Kirtan

Reputation: 21695

document.getElementById("iframeId").src = "Your URL here."

Upvotes: 14

SamGoody
SamGoody

Reputation: 14468

You can change the src of the iframe using two methods:

  1. Changing the href.
  2. 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' 

Upvotes: 1

Benbob
Benbob

Reputation: 14244

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

Upvotes: 1

mdja
mdja

Reputation: 705

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!

Upvotes: 1

Related Questions