改变iframe源即使用javascript [英] change iframe source in ie using javascript

查看:102
本文介绍了改变iframe源即使用javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的iframe如下所示:

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>

每当我点击< div> ,我必须更改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");    
}

这适用于Firefox,但不适用于Internet Explorer。

This works with Firefox, but not with Internet Explorer.

哪些代码也适用于Internet Explorer?

What code would work for Internet Explorer too?

推荐答案

你永远不应该使用'浏览器'检测',但功能检测。 imho最安全的方法是:

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

只测试src属性是可用的。如果没有,请在内容窗口上进行测试,最后一次尝试是setAttribute。

Just test if the src property is available. If not, test on content window, and the last try is setAttribute.

这篇关于改变iframe源即使用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆