Javascript Popup Window.Focus

时间:2011-12-26 22:18:50

标签: javascript popup

我有这个功能:

 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()时;弹出窗口仍然隐藏在主窗口后面,看起来我无法解决这个问题。

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

请查看代码。

由于

1 个答案:

答案 0 :(得分:0)

为什么不简单地创建一个弹出窗口;不要隐藏它;并在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('');
      }
    }
  });
}