change iframe source in ie using javascript

62,190

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

Solution 2

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

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

Solution 4

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.

Solution 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

Share:
62,190
Admin
Author by

Admin

Updated on April 13, 2020

Comments

  • Admin
    Admin about 4 years

    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?