[英]How can I focus() to another window on javascript, when the page loads?
On Google Chrome, I can only focus()
windows if an element has been clicked.在谷歌浏览器上,如果一个元素被点击,我只能使用
focus()
窗口。
How can I focus()
to a window using javascript, of which the popup window wasn't created inside the same window I'm using to focus the webpage如何使用javascript
focus()
到一个窗口,其中弹出窗口不是在我用来聚焦网页的同一窗口内创建的
var wnd = window.open("http://bing.co.uk", "stage");
Works fine for modifying popup windows that have been opened from a different webpage.适用于修改从不同网页打开的弹出窗口。
wnd.focus();
However this does not work as the stage window was already open, when the webpage with the wnd focus function was open.但是,这不起作用,因为当具有 wnd 焦点功能的网页打开时,舞台窗口已经打开。
If you know that a window with that name exists, you can get a reference to it from a later call to window.open
(eg, when you want to call blur
or focus
):如果您知道具有该名称的窗口存在,您可以从稍后对
window.open
调用中获取对它的引用(例如,当您想调用blur
或focus
):
window.open("", "theWindowName").focus();
But of course, if the window doesn't already exist, that will open a new window.但当然,如果窗口已经不存在,这将打开一个新窗口。 Sadly, you can't know in advance (except in your application logic) whether a window with that name already exists.
遗憾的是,您无法提前知道(除了在您的应用程序逻辑中)是否已经存在具有该名称的窗口。
Once you have the window object, you can call blur
or focus
.一旦你有了 window 对象,你就可以调用
blur
或focus
。
On Google Chrome, I can only
focus()
windows if an element has been clicked.在谷歌浏览器上,如果一个元素被点击,我只能使用
focus()
窗口。
If your code opened the window, you should be able to.如果您的代码打开了窗口,您应该可以。 For example:
例如:
HTML: HTML:
<button id="btnOpen">Open</button>
<button id="btnFocus">Focus</button>
JavaScript: JavaScript:
jQuery(function($) {
$("#btnOpen").click(function() {
window.open(
"about:blank",
"theWindow",
"width=500,height=500");
});
$("#btnFocus").click(function() {
// Note that this happens a full second later, NOT
// in direct response to a user event
setTimeout(function() {
window.open("", "theWindow").focus();
}, 1000);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.