js控制iframe弹出窗口

Amacoder 2012-04-12 08:53:50
有这样一种情况:
我在iframe里面嵌了一个网页(这个网页可能是来自网络)
如果网页里面含有像
<script>
function changelocation(){
parent.location.href="b.htm";
}
</script>
</head>

<body>
<a href="#" onclick="changelocation();">跳转</a>
</body>
这样的代码,那页面跳转之后就会覆盖掉了原来的iframe页,我想让这个跳转之后的
页面在iframe里面显示。但是只能是自己另外建一个js文件往这个网页上注入,
因为这个网页不一定是我写的,可能会是一个网络资源。
求解决办法。下面是需求的例子:
a.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">

<script>
function changelocation(){
parent.location.href="b.htm";
}
</script>
</head>

<body>
<a href="#" onclick="changelocation();">跳转</a>
</body>
</html>

b.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
bbbbbbbbbbbbbb
</body>
</html>

frame.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">

</head>

<body>
<center>
<iframe src="a.htm"/>
</center>
</body>
</html>
...全文
1060 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ixkixkix 2012-04-13
  • 打赏
  • 举报
回复
安全性问题,
最好不要这么搞,很多问题的
wzhiyuan 2012-04-12
  • 打赏
  • 举报
回复
楼主,这就是跨域问题
在以前的浏览器里(比如IE6),你通过在新窗口(无论是在iframe,或者window.open 或者说a target=_blank)打开的文档,
可以将父窗口重定位.
比如用户通过搜索引擎或者导航站点,打开新窗口到了你的网站,你一句window.opener.location.href="http//www.xxx.com" 就可以将父窗口重定向了,但是在现代浏览器里,这一功能被限制了(比如如果是自动运行脚本,就不能重定向,但是显示比如onclick可以重定向,或者弹出有警告)或者完全禁止了.
文盲老顾 2012-04-12
  • 打赏
  • 举报
回复
如果是引用跨域的页面,那你就没办法控制他的内容和操作了,安全性受限

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧