【问题标题】:change iframe url with javascript . firefox issue用 javascript 更改 iframe url。火狐问题
【发布时间】:2012-10-30 08:12:37
【问题描述】:

我有一个在 iframe http://mypage.com/myurl 中加载默认链接的主页。将它放在博客中,我想为每个帖子页面动态更改 iframe 内的默认链接。我像这样加载默认链接:

<iframe width='1000' height='500' name='iframename' frameborder='0'
        src='http:/mypage.com/myurl'></iframe>

(适用于所有浏览器)我通过将此代码放在每个帖子页面中来动态更改 iframe 内的链接:

<script type="text/javascript">
    document.iframename.location = "http://mypage.com/mynewurl"; 
</script>

它在 chrome 和 ie 上运行良好,但在 firefox 上不起作用!!

有什么想法或解决方法吗?

这里有一个简化的例子www.tinyurl.com/9l7zt2n 看看与firefox 的区别,它在iframe 中加载默认链接。 ie 和 chrome 可以随着添加的 javascript 而改变。

如果它有助于在博客www.tinyurl.com/9axhquh 上测试它,您可以看到它在 chrome 上运行,即,如果您单击任何帖子标题或“阅读更多”以加载帖子页面,顶部的 iframe 会相应更改

有什么想法或解决方法吗?

【问题讨论】:

    标签: javascript firefox iframe


    【解决方案1】:

    document.iframename.location 可能不支持跨浏览器。

    试试:

    <iframe width='1000' height='500' id='iframeid' frameborder='0' src='http:/mypage.com/myurl'></iframe>
    
    <script type="text/javascript">
        document.getElementById('iframeid').src = "http://mypage.com/mynewurl";
    </script>
    

    这意味着:

    1. 不支持通过document.elementName 语法按名称访问元素。
    2. iframe 元素没有名为location 的属性。 locationwindow 的属性。您可以使用contentWindow 访问iframewindow

    我在这里写了一个简短的测试http://jsfiddle.net/FyNW9/。在不同的浏览器中运行它。

    【讨论】:

    • 感谢@Roman 它确实有效,但如果我更改为 iframe id 而不是 iframe 名称,我在每篇文章中也有一些其他链接,格式为:mypage.com/mynewurl " target="iframeid">点击这里 mypage.com/mynewurl" target="iframeid"> 打开新窗口而不是在 ie 和 firefox 上的 iframe 内。在铬上它工作正常。这就是使用 iframename 的原因。有没有办法让这些链接也与 iframeid 一起使用?
    • 实际上通过添加一个类似的函数:function changeSource(newSource) {document.getElementById('iframeid').src = newSource;} 我得到的链接是:bing.com')">Click 这里
      这里的工作示例:tinyurl.com/92e6va5 所以感谢 Roman 现在无处不在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多