简体   繁体   English

当页面加载时,如何将焦点()聚焦到 javascript 上的另一个窗口?

[英]How can I focus() to another window on javascript, when the page loads?

The new question新问题

On Google Chrome, I can only focus() windows if an element has been clicked.在谷歌浏览器上,如果一个元素被点击,我只能使用focus()窗口。

The original question原来的问题

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 焦点功能的网页打开时,舞台窗口已经打开。

Answer to your original question:回答你原来的问题:

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调用中获取对它的引用(例如,当您想调用blurfocus ):

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 对象,你就可以调用blurfocus

Live Example |现场示例| Source来源


Answer to your completely different, new question:回答你完全不同的新问题:

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);
  });
});

Live Example |现场示例| Source来源

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

相关问题 当手机页面加载时,如何关注输入字段? - How can I focus on an input field when a phonegap page loads? 页面加载后,如何在Chrome中运行JavaScript代码段? - How can I run JavaScript snippets in Chrome when the page loads? 当页面使用Javascript加载时激活:focus - Activate :focus when the page loads with Javascript 页面加载时如何专注于特定选项卡 - how to focus on a specific tab when page loads 页面加载时如何关注输入框? - How to focus on input box when page loads? 如何在页面加载时停止javascript中的警报触发,并在单击按钮时使其运行? - How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked? 页面加载时,如何防止将注意力集中在表单(页面底部)上? - How do I prevent focus on form (at bottom of page) when page loads? 如何在页面加载时通过 javascript 制作视频全屏? - How can I make a video fullscreen through javascript when a page loads? 页面加载后如何执行javascript程序30 - how can I execute a javascript program 30 after page loads 我可以让 javascript 在该页面加载后自动单击另一个页面上的链接吗? - Can I get javascript to automatically click on a link on another page once that page loads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM