首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用闭锁打开和关闭窗口

用闭锁打开和关闭窗口
EN

Stack Overflow用户
提问于 2016-02-01 08:32:08
回答 2查看 66关注 0票数 1

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

我通过两种不同的功能实现了这一点。你们猜到了。一个是开放的,另一个是关闭的。

然而,这涉及到将myWindow 对象变成全局对象。为了避免这种情况,我想如果闭包可以方便地将对象限定为手动定义的函数。

我试着做一些类似下面粘贴的代码,但不确定如何让它工作。现在,publicClose还没有定义。

同样,我的重点是避免任何全球宣言。

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-01 09:11:41

这里来了一份工作副本

代码语言:javascript
复制
<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函数。

windowCenter函数中,myWindow不是一个新变量,而是来自openWindow上下文的变量。我也不会从windowCenter函数返回任何东西

publicClose上下文中删除openWindow,并在全局上下文中添加publicClose函数。但这是可以避免的,可以直接从按钮单击调用closeWindow

票数 0
EN

Stack Overflow用户

发布于 2016-02-01 08:49:05

如果您想拥有publicClose全局,就必须定义它为全局的;-)

代码语言:javascript
复制
var publicClose = {};

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

    publicClose = {
        close: closeWindow;
    }
}

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

代码语言:javascript
复制
    <li><button class="btn btn-default" onclick="publicClose.close()">Close Window</button></li>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35126009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档