开发者

Select and focus an already existing window

I run an e-commerce website, and I need to get this popup working when a client submits an order. Ideally, the popop would appear when the order success page loads, but popup blockers will stop this.

Instead, I generate the popup when the use clicks the "confirm order" button, but this obscures the 3DSecure page that the checkout redirects to before the order finishes.

To counteract this, I create the popup when the user clicks "confirm order", but instantly refocus the main window; a pop-under if you will. My plan is to refocus this new window from the order success page.

The issue is that I cannot find a way to get the object for an existing popup so I can run the focus on it. if I create the window using window.open(url,windowName,options), is there a way I can reference that from another page? Something along the lines of window.load(windowName) woul开发者_如何学Cd be ideal.


The signature of window.open is like this.

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

MDN notes that,

If a window with the name strWindowName already exists, then 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.

So this should work for you.

window.open('', 'windowName', '');

According to MDN whenever a window is opened a reference to it is created,

var windowObjectReference = window.open("http://www.google.com", "popup", "width=500,height=500");

You could always load it using that reference like

if(windowObjectReference != null || !windowObjectReference.closed) {
    windowObjectReference .focus();
}


This is tricky but this works on Chrome. First, open the window:

window.open('/test', 'testw', '');

Another link (even on another page), opens a 'page' in that same window by passing the same window name. The URL is JavaScript (so it's rather a hack):

window.open('javascript:void window.focus()', 'testw', '');

http://jsfiddle.net/pimvdb/KeHtp/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜