简体   繁体   English

如何在opener窗口后面放置一个用window.open()打开的窗口?

[英]How to put a window opened with window.open() behind the opener window?

I have a link that when clicked replaces the current page with another through the default <a> element click behaviour, but which also calls a JavaScript function to open another page in a new window. 我有一个链接,当单击该链接时,会通过默认的<a>元素单击行为将当前页面替换为另一个页面,但是该链接还会调用JavaScript函数以在新窗口中打开另一个页面。 I would like the new window opened from my JavaScript function to appear behind the current window but can't figure out how to do this. 我希望从JavaScript函数打开的新窗口出现在当前窗口的后面,但不知道如何执行此操作。 In actual usage, I am feeding click thrus to a database, the link that is controlled by the window.open statement. 在实际使用中,我将click thrus馈送到数据库,该数据库由window.open语句控制。 The other link is to the clients site in a new window. 另一个链接是在新窗口中的客户端站点。 I want the clients site to appear on top. 我希望客户网站显示在顶部。

My current code is as follows: 我当前的代码如下:

<script language="JavaScript" type="text/JavaScript">
    function countClicks(a,b)
    { 
       window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank");
    }
</script>

<a href="google.com" name="xy" onclick="javascript:countClicks(2,3);">test</a>

So for the example URLs shown above I would like the (original) window with Google to appear in front of the new window with StackOverflow, but the new window always opens in front. 因此,对于上面显示的示例URL,我希望带有Google的(原始)窗口出现在带有StackOverflow的新窗口的前面,但是新窗口始终在前面打开。

Popup behavior varies based on browser/user settings. 弹出行为因浏览器/用户设置而异。 I haven't been able to replicate the open behind behaviour myself, but I believe what you're looking for is .focus() . 我还无法自己复制行为背后开放性,但是我相信您正在寻找的是.focus()

Try: 尝试:

window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank").focus();

Try using window.focus() at the end of your existing function: 尝试在现有函数的末尾使用window.focus()

function countClicks(a,b) {
   window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank");
   window.focus();
}

That should bring the existing window back to the front, noting that behaviour may vary between browsers and depending on popup-blocking settings, etc. 这应该使现有窗口回到最前面,请注意,行为可能会因浏览器而异,并且取决于弹出窗口阻止设置等。

Or you could just swap the two urls, ie, put the StackOverflow url in the href with the appropriate query string specified directly rather than in JS, and put the Google url in the window.open() call. 或者,您可以只交换两个URL,即将StackOverflow URL放入具有直接指定的适当查询字符串的href ,而不是直接在JS中,并将Google URL放入window.open()调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM