JavaScript Remove Focus
In this section, you will learn how to remove focus from the window using JavaScript.
To remove the focus from the window, we have used blur() method of the Window object. It removes the focus from a window and moves it behind other windows. Now, to explain it more clearly, we have also used the focus() method of Window object which sets the focus to the specified window in front of other windows. When you load the 'removeFocus.html' code, you will get a button 'Set Focus'. On clicking the button, the setFocus() function is called and a new window will get open but the focus is on the 'removeFocus.html' page as we have used the method window.focus(). But on clicking the button 'Remove Focus' from the 'hello.html' page, the removeFocus() function is called and the focus will lost from the current window i.e. hello.html due to the window.blur() method and hence focus gets back to the 'removeFocus.html'.
Here is the code of removeFocus.html:
| <html> <h2>Set Focus</h2> <script type="text/javascript"> function setFocus(){ window.open("hello.html"); window.focus(); } </script> <button onclick="setFocus()">Set Focus</button> </html> |
Here is the code of hello.html:
| <html> <h2>Remove Focus</h2> <script> function removeFocus(){ window.blur(); } </script> <button onclick="removeFocus()">Remove Focus</button> </html> |
Output will be displayed as:

On clicking the button 'Set Focus', a 'hello.html' page will get open:
On clicking the button 'RemoveFocus' , you will move back to the 'removeFocus.html'
Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


