日期:2014-05-17  浏览次数:20396 次

js给图片修改src属性的兼容
我这个是在iframe里,iframe显示一张图片,我要通过父页面的url来判断显示不同的页面,下面的代码在IE里是可以的,为什么在谷歌和火狐里都不行,请问应该怎么修改?

var father_url = parent.location.href;
    var pic = document.getElementById("html_image");
    if (father_url == "http://www.abcd.com/html.html") {
        pic.src = "http://image15-c.poco.cn/mypoco/myphoto/20131024/23/6497733820131024232633033_640.jpg";
    }

------解决方案--------------------
你先alert一下parent.location.href有没有再火狐里面获取到值
如果有,在看看document.getElementById("html_image");有没有获取到,
------解决方案--------------------
调试一下看看有没有什么安全方面的警告
------解决方案--------------------
引用:
你先alert一下parent.location.href有没有再火狐里面获取到值
如果有,在看看document.getElementById("html_image");有没有获取到,



------解决方案--------------------
var father_url = parent.location.href;

我猜测 这个取不到。
改成如下可以实现图片的

$(function(){
var father_url = 'http://www.abcd.com/html.html';
var pic = document.getElementById("html_image");
if (father_url == "http://www.abcd.com/html.html") {
pic.src = "http://image15-c.poco.cn/mypoco/myphoto/20131024/23/6497733820131024232633033_640.jpg";
}
});