有这样一种情况:
我在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>

解决方案 »

  1.   

    楼主,这就是跨域问题
    在以前的浏览器里(比如IE6),你通过在新窗口(无论是在iframe,或者window.open 或者说a target=_blank)打开的文档,
    可以将父窗口重定位.
    比如用户通过搜索引擎或者导航站点,打开新窗口到了你的网站,你一句window.opener.location.href="http//www.xxx.com" 就可以将父窗口重定向了,但是在现代浏览器里,这一功能被限制了(比如如果是自动运行脚本,就不能重定向,但是显示比如onclick可以重定向,或者弹出有警告)或者完全禁止了.