Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between window.focus() and window.blur()?

Tags:

javascript

I'm new learner of JavaScript...I couldn't find out the difference between window.focus(); and window.blur();.

<!DOCTYPE html>
<html>
<body>
<button onclick="focus()">Click</button>
<script>
 function focus() {
var myWindow = window.open("", "", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
myWindow.focus();
}
</script>
</body>


When I am using them I cant find out any action on window by them....

Help me to find out the use of them ...:)

like image 750
Surya Teja Avatar asked Sep 02 '25 04:09

Surya Teja


1 Answers

They are basically opposite:

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

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

Examples:

-window.focus():

var myWindow = window.open("", "", "width=200, height=100"); // Opens a new window
myWindow.document.write("<p>A new window!</p>"); // Some text in the new window
myWindow.focus(); // Assures that the new window gets focus

-window.blur():

var myWindow = window.open("", "", "width=200, height=100"); // Opens a new window
myWindow.document.write("<p>A new window!</p>"); // Some text in the new window
myWindow.blur(); // Assures that the new window does NOT get focus
like image 106
user3378165 Avatar answered Sep 04 '25 18:09

user3378165



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!