【问题标题】:check if popup is open to give focus and if not to open it检查弹出窗口是否打开以提供焦点,如果不打开它
【发布时间】:2020-01-03 01:48:46
【问题描述】:

我知道有很多“检查弹出窗口是否打开”线程,我已经尝试了它们的所有版本,但我无法得到任何工作 - 如果这似乎是一个重复的问题,请原谅我。

基本上,我有一个打开弹出表单的按钮。通常用户会最小化弹出窗口。如果他们再次单击该按钮,则需要 a) 检查弹出窗口是否已经打开,如果是,请取消最小化它; b) 如果弹出窗口尚未打开,请打开它。

这是代码和按钮,因此很清楚它是如何设置的。我尝试了代码来检查 url 是否打开,但无法让它正常打开弹出窗口(如果它尚未打开)。它只会创建一个新选项卡。任何帮助将非常感激。

function stopwatch(myURL, title, myWidth, myHeight) {
  var left = (screen.width - myWidth) / 2;
  var top = (screen.height - myHeight) / 4;
  var myWindow = window.open(myURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + myWidth + ', height=' + myHeight + ', top=' + top + ', left=' + left);
}
<button class="shortbtnicon" type="button" onclick=" stopwatch ('https://example.com/shortcuts/stopwatch', 'MyPopUp', 450, 650);"><span><i class="fa fa-clock-o fa-1x"></i></button>

【问题讨论】:

    标签: javascript popup


    【解决方案1】:

    如果其他人需要解决方案,我终于解决了这个问题。

    var MyPopUp = false;
    function stopwatch(myURL, title, myWidth, myHeight){
      //checks to see if window is open
      if(MyPopUp && !MyPopUp.closed)
      {
        MyPopUp.focus(); //If already Open Set focus
      }
      else
      {
        var left = (screen.width - myWidth) / 2;
        var top = (screen.height - myHeight) / 4;
        MyPopUp = window.open(myURL, title,'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + myWidth + ', height=' + myHeight + ', top=' + top + ', left=' + left);//Open a new PopUp window
      }
    }

    【讨论】:

      猜你喜欢
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多