为什么我执行我保存window.open的返回值到NewWin变量中,但是为什么NewWin.close的值不能更改呢?
谢谢大家。代码如下:
<head>
<script>
var NewWin;function OpenNewWin(){
    NewWin=window.open('http://baidu.com','Newwin','toolbar=no,status=no,width=50,height=50');
}function CloseNewWin(){
    try{
       NewWin.close=MyClose;  //为什么这句话会报错呢???为什么下面的  NewWin.close()可以调用但是 不能更改NewWin.close呢??
}catch(e){
   alert(e.message);  //这里显示错误信息:对象不支持此操作
}
    NewWin.close();
}function MyClose(){
  alert('myclose');
}
</script>
</head>
<body>
<input type= "button" value = "打开一个新窗口" onClick = "OpenNewWin();">
Click this first
<p>
<input type = "button" value = "关闭新打开的窗口" onclick = "CloseNewWin();">  
Then click this
</body>

解决方案 »

  1.   

    补充: 假如我想更改NewWin.close,令它执行我自己的函数,应该怎么做呢?? 谢谢。
      

  2.   

    改成NewWin.window了 , 但是下面显示没有权限。为什么呢?
    <head>
    <script>
    var NewWin;function OpenNewWin(){
        NewWin=window.open('http://baidu.com','Newwin','toolbar=no,status=no,width=50,height=50');
    }function CloseNewWin(){
        try{
           NewWin.window.close=MyClose;  //改成NewWin.window了,但是下面显示没有权限.
    }catch(e){
       alert(e.message);  //这里显示“没有权限”
    }
        NewWin.close();
    }function MyClose(){
      alert('myclose');
    }
    </script>
    </head>
    <body>
    <input type= "button" value = "打开一个新出窗口" 
    onClick = "OpenNewWin();">
    Click this first
    <p>
    <input type = "button" value = "关闭新打开的窗口" 
    onclick = "CloseNewWin();">  Then click this
    </body>
      

  3.   

    显示没有权限就是 跨域问题了因为你访问了不在同一域名下的地址(baidu.com)换成自己的就OK
      

  4.   


    我就是需要跨域的,怎么办? 或者说,怎么让才能让IE开放跨域执行javascript? 
      

  5.   

    或者如果javascript本身不能开放跨域执行,那么假如我用VC的COM来执行javascript,怎么才能跨域执行? 谢谢!
      

  6.   

    建议你看下W3C Access Control for Cross - Site Requests specification
      

  7.   

    可以换种方法,先打开自已网站的页面,在这个页面是用ifarme嵌入要打开的页面,就好控制了。
    比如:A.html<html>
    <head>
    <script>
    var NewWin;function OpenNewWin(){
        NewWin=window.open('B.html?url=http://www.baidu.com','Newwin','toolbar=no,status=no,width=50,height=50');
    }function CloseNewWin(){
        try{
           NewWin.window.close = MyClose;  //为什么这句话会报错呢???为什么下面的  NewWin.close()可以调用但是 不能更改NewWin.close呢??
        }catch(e){
           alert(e.message);  //这里显示错误信息:对象不支持此操作
        }
        NewWin.close();
    }function MyClose(){
      alert('myclose');
    }
    </script>
    </head>
    <body>
        <input type= "button" value = "打开一个新窗口" onClick = "OpenNewWin();">
        Click this first
        <p>
        <input type = "button" value = "关闭新打开的窗口" onclick = "CloseNewWin();">  
        Then click this
        </p>
    </body>
    </html>
    B.html<html>
    <head>
    <title>无标题文档</title>
    <style type="text/css">
    html, body{ 
    margin:0; padding:0;
    }
    </style>
    <script type="text/javascript">
    window.onload = function(){
    var url = window.location.search.split("=")[1];
    var frm = document.getElementById("frm");
    frm.src = url;
    }
    </script>
    </head><body>
    <iframe id="frm" width="100%" height="100%" frameborder="0"></iframe>
    </body>
    </html>
      

  8.   

    Free_Wind22您好,所有html文件都是客户写好的,我的javascript是在BHO中注入网页中执行的,所以不能自己写html文件。我的目的是改写window.close,执行自己注入的js函数,但是遇到跨域,比如NewWin.window.close,就因为权限问题不能改写了,不知道怎么办。。
      

  9.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
    <script type="text/javascript">
    var NewWin;function OpenNewWin(){
        NewWin=window.open('http://baidu.com','Newwin','toolbar=no,status=no,width=50,height=50');
    }function CloseNewWin(){
        try{
           //NewWin.close=MyClose;  //为什么这句话会报错呢???为什么下面的  NewWin.close()可以调用但是 不能更改NewWin.close呢??
           NewWin.close(MyClose())/*这样可能达到你要的效果~~~,不信瞧瞧~~~*/
        }catch(e){
           /*可能window.close是一个内部方法,它的作用就是关闭窗口,可能不许客户端重新定义吧*/
           alert(e.message);  //这里显示错误信息:对象不支持此操作
        }
        //NewWin.close();
    }function MyClose(){
      alert('myclose');
    }
    </script>
    </head>
    <body>
    <input type= "button" value = "打开一个新窗口" onClick = "OpenNewWin();">
    Click this first
    <p>
    <input type = "button" value = "关闭新打开的窗口" onclick = "CloseNewWin();">  
    Then click this
    </body>
      

  10.   

    那就合并到一个文件:<html>
    <head>
    <script>
    var NewWin;function OpenNewWin(){
        NewWin=window.open('','Newwin','toolbar=no,status=no,width=50,height=50');
    var d = NewWin.window.document;
    d.write('<html><head><title>无标题文档</title><style type="text/css">\
    html, body{margin:0; padding:0;}</style>\
    </head><body><iframe id="frm" width="100%" height="100%" frameborder="0"></iframe></body></html>');
    d.close();
    var frame = d.getElementById("frm");
    frame.src = "http://www.baidu.com/";
    }function CloseNewWin(){
        try{
           NewWin.window.close = MyClose;  //为什么这句话会报错呢???为什么下面的  NewWin.close()可以调用但是 不能更改NewWin.close呢??
        }catch(e){
           alert(e.message);  //这里显示错误信息:对象不支持此操作
        }
        NewWin.close();
    }function MyClose(){
      alert('myclose');
    }
    </script>
    </head>
    <body>
        <input type= "button" value = "打开一个新窗口" onClick = "OpenNewWin();">
        Click this first
        <p>
        <input type = "button" value = "关闭新打开的窗口" onclick = "CloseNewWin();">  
        Then click this
        </p>
    </body>
    </html>
      

  11.   


    可能我没有描述清楚,
    包括OpenNewWin()和CloseNewWin()在内的所有的函数都是客户定义的,我不能更改,我只能注入一些新函数和执行自己注入的js代码,目的是在运行我自己注入的代码时替换window.close的方法,在不跨域的情况下没有问题,但是跨域了,就不能替换了,不知道为什么。谢谢。客户原来的代码,而且不一定叫NewWin这个名字:function OpenNewWin(){
        NewWin=window.open('http://baidu.com','Newwin','toolbar=no,status=no,width=50,height=50');
    }function CloseNewWin(){
        NewWin.close();
    }
      

  12.   


    您好,你改成 NewWin.close(MyClose())在跨域下都能执行,请问为什么?谢谢
      

  13.   


    +
    可以用iframe链接到跨域页面,然后控制iframe的父页面即可open的页面
      

  14.   

    NewWin.close(MyClose())
    /*先执行MyClose(),再执行NewWin.close*/
      

  15.   

    谢谢各位,问题我自己解决了。在bho中捕获DWebBrowserEvents2_WindowClosingEventHandler事件就可以了,这个事件在运行window.close()时触发。