dongzhou1901
2011-10-03 18:37
浏览 181
已采纳

从父iframe更改SRC属性

so, I use an iframe like this: <iframe src="script.php"></iframe> (with other attributes of course).

Now, what I am trying to do, is that the script.php does some things, than outputs a JS script, which should change that SRC attribue of the iframe to some other page.

Is that possible?

图片转代码服务由CSDN问答提供 功能建议

因此,我使用这样的iframe:&lt; iframe src =“script.php”&gt; &lt; / iframe&gt; (当然还有其他属性)。

现在,我要做的是,script.php做了一些事情,而不是输出 JS脚本,它应该将iframe的SRC属性更改为其他页面。

这可能吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dousong8187 2011-10-03 18:39
    已采纳

    Assume only one frame, without id or name:

    document.getElementsByTagName("iframe")[0].src = "http://new.url";
    

    If the current frame location is at the same domain:

    frames[0].location.href = "http://new.url";
    

    If you have multiple frames, it's very wise to attach an ID to it. When you want to access the frame through the frames object, set a name:

    <iframe id="myframe" name="a_frame_name" src="script.page"></iframe>
    
    JavaScript:
    document.getElementById("myframe").src = "newpage.php";
    frames.a_frame_name.location.href = "newpage.php";
    frames["a_frame_name"].location.href = "newpage.php";
    

    Note: frames[..].location.href can only be changed if the main page and the frame's location are at the same host, due to the Same Origin policy.

    点赞 打赏 评论

相关推荐 更多相似问题