I’m struggling to make the code blow work…
Then, I’m wondering why window.focus() and window.blur() methods are not working / only working from input button. They are not working when I call from setInterval().
For example, in the case below, when I push the button in HTML, it makes window at first time, and then focus the window from second time I press the button.
However, I also set setInterval() to make the window focus but it doesn’t work. I can see the log on console, so setInterval function works correctly but win.focus() is ignored by somehow.
<head>
...
<script>
var win;
function makePopup(){
if (!win || win.closed) {
win = window.open("","","width=200, height=200");
win.blur();
}else{
win.focus();
console.log("opened");
}
}
setInterval(function(){makePopup();},4000);
</script>
</head>
<body>
<input type="button" onclick="makePopup()"/>
</body>
So the problems are:
- window.blur() doesn’t work at all.
- window.focus() works only when I call makePopup() from the HTML button and doesn’t work from setInterval().
I’m testing in Chrome and Safari with Mac OSX.
If you could give me some suggestion, I really appreciate it.
Thanks in advance.
The browser knows the difference between someone clicking a link to open a pop-up and when it is triggered by a script. In order to prevent malicious activity many browsers prevent scripted pop-ups.
By specifying a URL that is on the same domain as the hosted page, it should work.