为什么window.close不能更改?

calvin_guo 2010-08-07 05:32:12
为什么我执行我保存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>
...全文
163 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
calvin_guo 2010-08-12
  • 打赏
  • 举报
回复
谢谢各位,问题我自己解决了。在bho中捕获DWebBrowserEvents2_WindowClosingEventHandler事件就可以了,这个事件在运行window.close()时触发。
2010-08-09
  • 打赏
  • 举报
回复
那就合并到一个文件:

<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>
hlq_zist 2010-08-09
  • 打赏
  • 举报
回复

<!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>

calvin_guo 2010-08-09
  • 打赏
  • 举报
回复
Free_Wind22您好,所有html文件都是客户写好的,我的javascript是在BHO中注入网页中执行的,所以不能自己写html文件。我的目的是改写window.close,执行自己注入的js函数,但是遇到跨域,比如NewWin.window.close,就因为权限问题不能改写了,不知道怎么办。。
root_lee 2010-08-09
  • 打赏
  • 举报
回复
9L的方法可以
亥亥 2010-08-09
  • 打赏
  • 举报
回复
没怎么明白
2010-08-09
  • 打赏
  • 举报
回复
可以换种方法,先打开自已网站的页面,在这个页面是用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>

guilipan 2010-08-09
  • 打赏
  • 举报
回复
建议你看下W3C Access Control for Cross - Site Requests specification
calvin_guo 2010-08-09
  • 打赏
  • 举报
回复
或者如果javascript本身不能开放跨域执行,那么假如我用VC的COM来执行javascript,怎么才能跨域执行? 谢谢!
calvin_guo 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 vvviop 的回复:]

显示没有权限就是 跨域问题了

因为你访问了不在同一域名下的地址(baidu.com)

换成自己的就OK
[/Quote]

我就是需要跨域的,怎么办? 或者说,怎么让才能让IE开放跨域执行javascript?
vvviop 2010-08-09
  • 打赏
  • 举报
回复
显示没有权限就是 跨域问题了

因为你访问了不在同一域名下的地址(baidu.com)

换成自己的就OK
calvin_guo 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mowanglijiang 的回复:]

改成 NewWin.window.close
[/Quote]

[Quote=引用 3 楼 xk1126 的回复:]

NewWin.window.close
[/Quote]

改成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>
hlq_zist 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 calvin_guo 的回复:]

引用 13 楼 hlq_zist 的回复:

HTML code

<!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"……
[/Quote]


NewWin.close(MyClose())
/*先执行MyClose(),再执行NewWin.close*/
hoojo 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hlq_zist 的回复:]

HTML code

<!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 Open……
[/Quote]

+
可以用iframe链接到跨域页面,然后控制iframe的父页面即可open的页面
calvin_guo 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hlq_zist 的回复:]

HTML code

<!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 Open……
[/Quote]

您好,你改成 NewWin.close(MyClose())在跨域下都能执行,请问为什么?谢谢
calvin_guo 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 free_wind22 的回复:]

那就合并到一个文件:
HTML code

<html>
<head>
<script>
var NewWin;

function OpenNewWin(){
NewWin=window.open('','Newwin','toolbar=no,status=no,width=50,height=50');
var d = NewWin.window.document;
……
[/Quote]

可能我没有描述清楚,
包括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();
}

xk1126 2010-08-07
  • 打赏
  • 举报
回复
NewWin.window.close
上海程序员3 2010-08-07
  • 打赏
  • 举报
回复
改成 NewWin.window.close
calvin_guo 2010-08-07
  • 打赏
  • 举报
回复
补充: 假如我想更改NewWin.close,令它执行我自己的函数,应该怎么做呢?? 谢谢。

87,910

社区成员

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

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