Mobile Safari and iFrame src attribute



我正在尝试为iFrame设置"src"属性。它在FireFox和Internet Explorer上运行良好。然而,当在iPad上测试移动safari时,更改"src"属性没有任何作用。

我有一个iFrame,它在HTML中设置了"src"属性。

<iframe id="iFrame0" style="margin: 0px; overflow: hidden; padding: 0px; height:80px; width:500px" src='.../loading.gif' frameborder="0"></iframe>

稍后,我有一些代码试图更改src

var iFrame0 = YAHOO.util.Dom.get('iFrame0');
YAHOO.util.Event.addListener(iFrame0, 'load', function() { alert('test'); });
MyWebService.GetDynamicUrl('someparam', function(url) {
  iFrame0.src = url;
});

事件不仅不会触发,而且URL的内容也不会更改。在测试中,我注意到值iFrame0.src确实会更改为新传入的URL,但页面上的内容不会更改。

然而,我使用YUI来消除测试中的一个潜在问题,我也尝试通过直接访问iFrame

document.getElementById('iFrame0').attribute("src") = '..../newurl.gif';

仍然不起作用。

最后,我通过动态创建iframe并将其附加到DOM来解决这个问题。我还为id和src属性添加了一个时间戳,以确保没有进行缓存(尽管我不确定这是否真的有必要)。

var elIFrame = document.createElement("iframe");
var dt = new Date();
elIFrame.src = APP_IMAGEPATH + "/loading.gif?dt=" + dt.getTime();
elIFrame.id = 'newCard2' + dt.getTime();
elIFrame.frameBorder = 0;
elIFrame.scrolling = "no";
elIFrame.style.width = "500px";
elIFrame.style.height = "1.8em";
YAHOO.util.Dom.insertAfter(elIFrame, this.pre + "cardMask");

您确定变量iFrame0实际指向的是iFrame DOM对象,而不是某个空对象或使用相同ID的其他元素(例如div)吗?也许你可以试着检查初始src,看看它是否是你想要的(即"…/loading.gif")。

尝试使用以下内容访问帧:

var frameObj = document.frames ? document.frames['iFrame0'] : document.getElementById('iFrame0'),
    frameWin = frameObj.contentWindow || frameObj;

然后尝试修改它的src:

frameWin.src = '..../newurl.gif';

最新更新