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

从父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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧