Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript window.open() Method
Next article icon

Javascript Window Blur() and Window Focus() Method

Last Updated : 31 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript provides various methods to manipulate browser windows, two of which are window.blur() and window.focus(). These methods are particularly useful when managing the user interface and enhancing the user experience by controlling window focus.

Javascript Window Blur()

Javascript Window Blur() method is used to remove focus from the current window. i.e, It sends the new open Window to the background. 

Syntax:

Window.blur()

Parameter: It does not require any parameters. 

Return Value: It does not return any value. 

Below examples illustrate the window.blur() method of JavaScript:

Example: The below example illustrates the window.blur() method in JavaScript:

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title> window.blur() method of JavaScript</title> </head>  <body>     <h1 style="color: green">GeeksforGeeks</h1>     <h2>JavaScript window.focus() Method</h2>     <script>         let Window;          // Function that open the new Window         function windowOpen() {             Window = window.open(                 "https://www.geeksforgeeks.org/",                 "_blank", "width=400, height=450 top=75");         }          // function that Closes the open Window         function windowClose() {             Window.close();         }          //function that focus on open Window         function windowFocus() {             Window.focus();         }     </script>     <button onclick="windowOpen()">         Open GeeksforGeeks     </button>     <button onclick="windowFocus()">         Focus GeeksforGeeks     </button>     <button onclick="windowClose()">         Close GeeksforGeeks     </button> </body>  </html> 

Output: If click on the blur GeeksforGeeks button then the geeksforgeeks.org page will move to the background 

ouput1

JavaScript Window.focus()

JavaScript Window.focus() method is used to focus on the new open window. i.e bringing back the blurred window to the foreground. 

Syntax:

window.focus()

Parameter: It does not require any parameters. 

Return Value: It does not return any value.

Example: The example below illustrates the window.focus() method in javascript.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <title> window.blur() method of JavaScript</title> </head>  <body>     <h1 style="color:green" class="gfg">GeeksforGeeks</h1>     <h2>Focus</h2>     <script>         let Window;          // Function that open the new Window         function windowOpen() {             Window = window.open(                 "https://www.geeksforgeeks.org/",                 "_blank", "width=400, height=450 top=75");         }          // function that Closes the open Window         function windowClose() {             Window.close();         }          //function that focus on open Window         function windowFocus() {             Window.focus();         }     </script>     <button onclick="windowOpen()">         Open GeeksforGeeks     </button>     <button onclick="windowFocus()">         Focus GeeksforGeeks     </button>     <button onclick="windowClose()">         Close GeeksforGeeks     </button> </body>  </html> 

Output: if click on the focus GeeksforGeeks button then the geeksforgeeks.org windows will come to the foreground.  

ouput2

Differences Between window.blur() and window.focus()

Featurewindow.blur()window.focus()
FunctionalityRemoves focus from the current window, sending it to the backgroundBrings the current window to the foreground, ensuring it has the user’s attention
Common Use CaseUsed to allow users to interact with other windows or applications without closing the current windowUsed to alert users to important information or actions required in the current window
User ExperienceThe window remains open but is no longer the active windowThe window becomes the active window, taking precedence over others
Typical ScenariosOpening pop-up windows or new tabs without disturbing the user’s current taskBringing notification or confirmation windows to the user’s immediate attention
Effect on UserUser’s attention is diverted to another window or applicationUser’s attention is brought back to the current window

Conclusion

The window.blur() and window.focus() methods are essential tools for managing window focus in JavaScript. They allow developers to control the user’s interaction with multiple windows, enhancing the overall user experience. For a more comprehensive understanding of JavaScript and its various features, refer to the JavaScript Cheat Sheet – A Basic Guide to JavaScript.

Supported Browser: The browser is supported by Window Blur() and Window Focus() Methods are listed below:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 12.1 and above
  • Safari 1 and above


Next Article
JavaScript window.open() Method

N

Naman_Garg
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions

Similar Reads

  • Javascript Window Open() & Window Close() Method
    The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values assigned to the parameter. Syntax: window.open(URL, name, specs, replace)Parameters: This method accepts four parameters as mentioned above and describe
    3 min read
  • JavaScript window.open() Method
    The Javascript window.open() method is used to open a new tab or window with the specified URL and name. It supports various parameters that can be used to specify the type and URL location of the window to be opened. Syntax: window.open(url, windowName, windowFeatures)Parameters: It has the followi
    3 min read
  • JavaScript BOM Window Screen
    The Browser Object Model (BOM) is a browser-specific convention referring to all the objects exposed by the web browser. The BOM allows JavaScript to "interact with" the browser. The object of the window represents a browser window and all its corresponding features. A window object is created autom
    2 min read
  • Window Object in JavaScript
    In JavaScript, the Window object represents the browser window that contains a DOM document. The Window object offers various properties and methods that enable interaction with the browser environment, including manipulating the document, handling events, managing timers, displaying dialog boxes, a
    5 min read
  • How to close current tab in a browser window using JavaScript?
    In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method.  Syntax:window.close();But accounting for a secu
    1 min read
  • Set the focus to HTML form element using JavaScript
    To set focus to an HTML form element, there can be many methods that can be used to focus. In this article, we will discuss them all. These are the following approaches: Table of Content Using focus() methodUsing window.onload methodApproach 1: Using focus() methodIn this approach, we are going to u
    2 min read
  • How to create a Blur Mask Image Website using HTML CSS and JavaScript ?
    In this article, we will see how to create a website with a blur mask image using HTML, CSS, and JavaScript. Generally, we often see this kind of effect on many websites. When clicking the button, the box immediately becomes hidden, displaying the blurred content. The website is responsive and works
    4 min read
  • Angular PrimeNG Tooltip Focus and Blur
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the Tooltip Focus and Blur in Angular PrimeNG. Angular Prime
    3 min read
  • HTML DOM Window resizeTo() Method
    The HTML Window resizeTo() Method is used to resize a window to the specified width and height. Syntax: window.resizeTo(width, height) Parameters: width: Sets the width of the window, in pixels.height: Sets the height of the window, in pixels. Example: In this example, we will resize the window by u
    1 min read
  • Implement a JavaScript when an element loses focus
    Given a document, the task is to implement functionality when the element loses focus. We have 2 options, one is the onblur event and another is onfocusout event JavaScript. We're going to discuss a few methods. First few methods to understand. Approach 1: Using onblur event JavaScript onblur Event:
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences