断狱丹阳 2015-08-08 08:00 采纳率: 0%
浏览 1899

小白很是迷惑,求大神解救(关于setTimeout()和open())

<!--------------------------html代码-------------------------->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>windowOpen</title>
      <script type="text/javascript" src="case.js"></script>
    </head>
    <body>
     <a href="javascript:void(null)" onclick="openWin()">点击弹出新窗口</a>
    </body>
    </html>
<!-----------------------javascript代码---------------------->
        var win;
        function openWin(){
        var width=400;
        var height=300;
        var hcenter=(screen.width-width)/2;
        var vcenter=(screen.height-height-50)/2;
        var name="open"
        var appearance="width="+width+",height="+height+",left="+hcenter+",top="+vcenter;
        win=window.open("",name,appearance);
        win.document.write("这是脚本open()函数打开的新窗口!") 
        win.setTimeout("win.close()",2000); 
        //win.setTimeout("self.close()",2000);
        //window.setTimeout("win.close()",2000);
        //window.setTimeout("self.close()",2000);
    }
        //window.setTimeout("win.close()",2000);

问题一:在浏览器中测试,得到如下结果,为什么win和self不是同一个窗口对象的引用?
win.setTimeout("win.close()",2000); //ff,IE11中均无效,新窗口不会定时关闭
win.setTimeout("self.close()",2000); //ff,IE11中均有效,新窗口会定时关闭

问题二:以下两行代码有不同的结果,为什么window调用setTimeout()却能对win起作用?
window.setTimeout("win.close()",2000); //ff,IE11中均有效
window.setTimeout("self.close()",2000);//在IE11中有效,但关闭的不是新窗口,ff中无效

问题三:在函数openWin()之外,
window.setTimeout("win.close()",2000);
这行代码也可以定时关闭新窗口,但是第一次定时关闭窗口后,
接下来打开的新窗口都不会定时关闭,除非先刷新再打开新窗
口,这也不知为何?

  • 写回答

3条回答 默认 最新

  • lpfly 2015-08-08 09:06
    关注

    setTimeout()调用的是函数,你这边用字符串是错的。
    W3C的例子:

    setTimeout(function(){ alert("Hello"); }, 3000);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题