为什么我执行我保存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>
谢谢大家。代码如下:
<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>
解决方案 »
- JQUERY小问题,求助大家!!
- 页面初试加载时候获取不到window的高和宽,必须刷新或resize才行
- 问个超级简单问题....
- *************************************function now(){return +new Date;)?????
- 如何关闭指定名称的窗口
- document的三个方法我怎么没见体现出来啊?郁闷。。。。。
- 100分求javascript教程(最好很详细和深入)(dama2003,借同学分来发的,马上给分)
- window.close()在框架中怎么就不起作用了
- 如何实现增加一行的操作
- Javascript中的精度问题?为什么45.6*13=592.8000000000001,而86217.8+45.6=86263.40000000001??
- 给份javascritp提交表单的范例,带文件的最好,谢谢!
- 百分求正则表达式。请求高手帮助。
<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>
我就是需要跨域的,怎么办? 或者说,怎么让才能让IE开放跨域执行javascript?
比如: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>
<!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>
<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>
可能我没有描述清楚,
包括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();
}
您好,你改成 NewWin.close(MyClose())在跨域下都能执行,请问为什么?谢谢
+
可以用iframe链接到跨域页面,然后控制iframe的父页面即可open的页面
/*先执行MyClose(),再执行NewWin.close*/