打开和关闭窗户与封闭

时间:2016-02-01 08:32:08

标签: javascript closures

我想点击两个按钮打开和关闭一个窗口。 打开按钮打开,关闭按钮关闭窗口。简单!

我通过两个独立的功能来实现这一目标。你们猜对了一个用于开放,另一个用于关闭。

但是,这涉及使myWindow对象全局化。为了避免这种情况,我在想闭包是否可以派遣myWindow对象到手动定义的函数。

我尝试过像下面粘贴的代码,但不知道如何让它工作。现在publicClose未定义。

同样,我的重点是避免任何全球声明。

<html>
 <head>
 <meta charset="UTF-8">
 <title>Day 5-6</title>
 <link rel="stylesheet" href="css/styles.css">
</head>
<body>
 <div class="buttons">
    <ul class="list-inline">
        <li><button class="btn btn-default" onclick="openWindow()">Open Window</button></li>
        <li><button class="btn btn-default" onclick="publicClose()">Close Window</button></li>
    </ul>
</div>

 <script type="text/javascript">


    function openWindow(){
        var myWindow = windowCenter("", "", 400, 200 );

        function windowCenter(url, title, w, h) {
        // Fixes dual-screen position                         Most browsers      Firefox
            var ScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
            var ScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;

            width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
            height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

            var left = ((width / 2) - (w / 2)) + ScreenLeft;
            var top = ((height / 2) - (h / 2)) + ScreenTop;
            var myWindow = window.open("", "", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            myWindow.document.write("<strong>I am child window.</strong>")
            // Puts focus on the newWindow
            if (window.focus) {
                myWindow.focus();
            }
        }
        function closeWindow(){
            myWindow.close();

        }
        var publicClose = {
            close: closeWindow;
        }

        return publicClose;


    }




  </script>
  </body>
 </html>

2 个答案:

答案 0 :(得分:0)

如果您希望全局publicClose,则必须将其定义为全局; - )

var publicClose = {};

function openWindow(){
    // --- your code from above here ---

    publicClose = {
        close: closeWindow;
    }
}

此外,您的关闭函数调用是错误的。修正:

    <li><button class="btn btn-default" onclick="publicClose.close()">Close Window</button></li>

答案 1 :(得分:0)

这是一份工作副本

<title>Day 5-6</title>

<script type="text/javascript">
  var closeWindow;  
  function openWindow() {
    var myWindow;
    windowCenter("", "", 400, 200);

    function windowCenter(url, title, w, h) {
      // Fixes dual-screen position                         Most browsers      Firefox
      var ScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
      var ScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;

      var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
      var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

      var left = ((width / 2) - (w / 2)) + ScreenLeft;
      var top = ((height / 2) - (h / 2)) + ScreenTop;
      myWindow = window.open("", "", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
      myWindow.document.write("<strong>I am child window.</strong>");
        // Puts focus on the newWindow
      if (window.focus) {
        myWindow.focus();
      }
    }

    closeWindow = function() {
      myWindow.close();
    }

  }

  function publicClose() {
      closeWindow();
  }

</script>

<body>
  <div class="buttons">
    <ul class="list-inline">
      <li>
        <button class="btn btn-default" onclick="return openWindow();">Open Window</button>
      </li>
      <li>
        <button class="btn btn-default" onclick="publicClose()">Close Window</button>
      </li>
    </ul>
  </div>


</body>

有什么变化? 而不是返回一个对象,我将一个关闭窗口分配给一个全局变量,它创建了一个openWindow函数的clousure。

windowCenter函数内,myWindow不是新变量,而是来自openWindow上下文的变量。我不会从windowCenter函数

返回任何内容

publicClose上下文中删除了openWindow,并在全局上下文中添加了publicClose函数。但是可以避免这种情况,可以通过按钮点击直接调用closeWindow