【问题标题】:Javascript Popup Window.FocusJavascript Popup Window.Focus
【发布时间】:2011-12-26 22:18:50
【问题描述】:

我有这个功能:

 var replyTo = null;
 var windWidth = 730;
 var windHeight = 550;
 var windTop = parseInt((screen.availHeight - windHeight) / 3);
 var windLeft = parseInt((screen.availWidth - windWidth) / 2);

 function windowPreOpen() {
     replyTo = window.open('', 'Connect With Twitter', 'width=' + windWidth + ', height=' + windHeight + ', left=' + windLeft + ', top=' + windTop + ', scrollbars, resizable');
     window.focus();
 };

 function makeReplyTo() {
     windowPreOpen();
     var user_id = "3";
     var data = $.ajax({
         type: "POST",
         url: "uspolitics_pulse/functions.php",
         data: {
             type: 'checkOauth',
             user_id: user_id
         },
         success: function (data) {
             if (data) {
                 replyTo = window.open(data, 'Connect With Twitter', 'width=' + windWidth + ', height=' + windHeight + ', left=' + windLeft + ', top=' + windTop + ', scrollbars, resizable');
                 replyTo.focus();
             } else {
                 replyTo.close()
                 replyTo = $.prettyPhoto.open('');
             }
         }
     });
 }

它包含一个避免弹出窗口阻止程序阻塞我的弹出窗口的小技巧。 它首先打开一个空的弹出窗口,然后用正确的弹出窗口替换它。

问题是我必须隐藏主窗口下的第一个弹出窗口,然后专注于新的。

但是当我尝试使用replyTo.focus();弹出窗口仍然隐藏在主窗口后面,看起来我无法解决这个问题。

有没有办法重新关注弹出窗口??

请查看代码。

谢谢

【问题讨论】:

  • 正是出于这种原因,我们通常避免使用真实的窗口/弹出窗口。我看到你正在使用 jQuery,所以你真的应该考虑只使用 jQueryUI 对话框 (jqueryui.com/demos/dialog)。

标签: javascript popup


【解决方案1】:

为什么不简单地创建一个弹出窗口;不要隐藏它;并在 XHR 返回时更新其位置?即类似:

var replyTo = null;
var windWidth = 730;
var windHeight = 550;
var windTop = parseInt((screen.availHeight - windHeight) / 3);
var windLeft = parseInt((screen.availWidth - windWidth) / 2);

function windowPreOpen() {
  replyTo = window.open('', 'Connect With Twitter', 'width=' + windWidth + ', height=' + windHeight + ', left=' + windLeft + ', top=' + windTop + ', scrollbars, resizable');
  window.focus();
};

function makeReplyTo() {
  windowPreOpen();
  var user_id = "3";
  var data = $.ajax({
    type: "POST",
    url: "uspolitics_pulse/functions.php",
    data: {
      type: 'checkOauth',
      user_id: user_id
    },
    success: function (data) {
      if (data) {
        replyTo.location = data;
      } else {
        replyTo.close();
        replyTo = $.prettyPhoto.open('');
      }
    }
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多