开发者

Check popup window status

When someone requests a chat, an entry is made in the database. I have an hidden iframe on our dashboard that checks the database every 20 seconds to see if there is a chat and if there is it launches a popup window. Even if the popup is open the iframe still refreshes the popup every 20 seconds. Want I am trying to achieve is a javascript to check the status of the popup. If it is closed I want it to reopen it... if it is open then it bring it into focus... but I dont want the popup to refresh.. as I have an ajax script doing this..

Here is my code:

<script lan开发者_如何学Pythonguage="javascript" type="text/javascript">
function myOpenWindow(winURL, winName, winFeatures, winObj)
{
     var theWin;
     if (winObj != null)
     {
          if (!winObj.closed)
          {
               winObj.focus();
               return winObj;
          }
     }
     else
     {
          theWin = window.open(winURL, winName, winFeatures);
          return theWin;
     }
} 
</script>
<% IF ChatSessionID <> "" THEN %>
<script language="javascript" type="text/javascript">
var gmyWin = null;
window.onload = function()
{
     var w = 900;
     var h = 500;
     var l = (screen.width-w)/2;
     var t = (screen.height-h)/2;
     var params = 'status=0,resizable=0,scrollbars=0,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t;
     gmyWin = myOpenWindow("/chat/chat_window.asp?ChatSession=<%=ChatSessionID%>&id=3", "myWin", params, gmyWin)
}
</script>
<% END IF %>

Any help you could provide would be greatly appreciated..

Best Regards, Paul


I am not sure, but I believe if you name the window (e.g. myWin) when you call window.open, then later call window.open again using the same name, it will return the existing window if its already open or open/re-open the window and return a handle to that.

Edit

Ah, there you go -- from window.open:

If a window with the name strWindowName already exists, then, instead of opening a new window, strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. If you want to open a new window on every call of window.open(), you should use the special value _blank for strWindowName.

I believe according to the above mentioned specs, this might work:

var hChatWindow = window.open("", "ChatWindow", "whatever features"); // url intentionally left blank
// hChatWindow now contains a reference to new, existing or re-opened window
hChatWindow.focus();
if (hChatWindow.location=="about:blank") {                            // not sure; you need to experiment here
    hChatWindow.location = "/chat/chat_window.asp?whatever";
}

Demo here, source here.


Register a callback on gmyWin.onunload.


You will find it tricky to subvert "block pop-up windows" in most browsers. However, if it is disabled, the following will work.

Main window:

var status = false;

function winOpen(){
    window.open("child.html");
}

function winStatus(){
    alert(status);
}

Pop-up window:

window.opener.status = true;

window.onblur = window.focus;

window.onunload = function(){
    window.opener.status = false;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜