Window focus() 方法

实例

Assure that the new window GETS focus (send the new window to the front):

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>
  5. <button onclick="myFunction()">Try it</button>
  6. <script>
  7. function myFunction() {
  8. var myWindow = window.open("", "", "width=200,height=100");
  9. myWindow.document.write("<p>A new window!</p>");
  10. myWindow.focus();
  11. }
  12. </script>
  13. </body>
  14. </html>

More “Try it Yourself” examples below.


定义与用法

The focus() method sets focus to the current window.Tip: Use theblur() method to remove focus from the current window.Note: This method makes a request to bring the current window to the foreground. It may not work as you expect in all browsers, due to different user settings.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
focus()YesYesYesYesYes

语法

  1. window.focus()

参数

None

返回值

No return value

更多实例

实例

Assure that the new window does NOT get focus (send the new window to the background):

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>Click the button to open a new window, and assure that the new window does NOT GET focus (send the new window to the background).</p>
  5. <button onclick="myFunction()">Try it</button>
  6. <script>
  7. function myFunction() {
  8. var myWindow = window.open("", "", "width=200, height=100");
  9. myWindow.document.write("<p>A new window!</p>");
  10. myWindow.blur();
  11. }
  12. </script>
  13. </body>
  14. </html>

分类导航