开发者

Change the SRC attribute from a parent iframe

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

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜