比如,我有一个页面a.html<html>
<title>I want to prevent being redirected</title>
<body>
<a href="b.html" target="_blank">open window b</a>
</body>
<html>当点击上面的链接后,打开b.html<html>
<title>I will redirect opener</title>
<script language="JavaScript">
function redirect()
{
    window.opener.open("http://www.163.com", '_self');
}
</script>
<body onload="redirect()">
I will redirect opener to www.163.com
</body>
<html>b.html会调用window.opener.open 或 也可通过window.opener.location来把a.html重定向到别的网站。【问题来了】 请问在a.html有没有什么办法进行限制,使得b.html中的window.opener.open无效?

解决方案 »

  1.   

    window.opener = null?
    这样不可以吗
      

  2.   

    这个很难可在 beforeunload 中判断 有没有点击过窗口,或者按过alt+f4  如果没有是通过 close 或者 location 
    或者子窗口 用 window.opener 来导致的重定向!
    不过在firefox 中 beforeunload 的 event 值,是判断不了那些情况的!IE的可以!
    if(window.addEventListener){
                   window.addEventListener('beforeunload', function(e){ },false); 
              }else{
                   window.attachEvent('onbeforeunload',function(){  });   
             } 
      

  3.   

    <a href="javascript:window.open('Untitled-1.html').opener=null;void 0;">open window b</a> 
      

  4.   

    又抬头一看,楼猪勳章栏赫然两个VC/MFC大板内专家分第一名、第三名,我震惊了。
    膜拜下真正的系统程序员。
      

  5.   

    我有个想法,就是通过url传值得方式进行控制。
    仅供参考,没测试a页面
    要是想定向
    <a href="b.html?state=1" target="_blank">open window b</a>
    不定向
    <a href="b.html?state=1" target="_blank">open window b</a>
    function redirect()
    {
        var state=location.href.split('=');
        if(state[1]==1)
        {
             window.opener.open("http://www.163.com", '_self');
        }
        else
        {
             return false;
        }
    }
      

  6.   

    不定向应该是不传值
    <a href="b.html" target="_blank">open window b</a>
      

  7.   

    to楼上的各位,
    感谢各位的回复。
    不好意思,今天比较忙,没有来得及回复哈~
    本人js比较菜哈,但是又想出两道笔试题 ^_^第二个问题来了,在a.html中,不修改window.open('Untitled-1.html').opener=null;的前提下即确保b.html中的window.opener是一个window对象的前提下,能否在a.html中设置,使得b.html中的
    window.opener.open方法无效。 但是window.opener.status="xxxx"有效 
      

  8.   

    去试试重载window.opener.open()吧,把它屏了.
      

  9.   

    window.open('Untitled-1.html').opener.open = function(){alert('cannot redirect');};