How to focus on a pop-up every 1 minute?

Asked

Viewed 725 times

2

How to focus on a pop-up every 1 minute? Remembering that the pop-up is minimized and I want to leave it "in evidence" on the user screen.

  • as far as I know the only way to focus on a window is by giving an Alert()

2 answers

6

The way to do this is with the function focus of the object window returned by window opening.

For example:

var janela = window.open('http://www.google.com');
setInterval(function() { 
    janela.focus();
}, 60000);

The problem is that this does not seem to work in several browsers, after all they want to prevent malicious scripts from focusing the user on a particular window, such as an advertisement, for example.

Tests carried out:

  • Chrome: not working (no errors)
  • Firefox: did not work (no errors) and popup opened a new tab
  • Internet Explorer: it worked

Therefore, my suggestion is to avoid the use of popup. First because often this is an attempt to replicate desktop program functionality in web applications. Second because I always see this causing many side effects and bumping into several limitations, such as the case of Firefox, which opened the popup in a new tab.

Think of an alternative solution, like the notifications here from Stackoverflow or Facebook, for example.


Update

I tested an alternative, reopening the popup in the given interval:

setInterval(function() { 
    window.open('http://www.google.com', 'minha_janela').focus();
}, 60000);

However, the result is the same in the various browsers, that is, only IE gives the focus back to the popup. The difference is that the page is updated at each opening and, if the popup has been closed, it is reopened.

  • 2

    IE, as always, leading the browsers. How about a little Thepiratebay? onclick=function(){/* o código acima*/; onclick=null}; (tip: don’t do it)

  • 1

    Seriously now, how about using the Notification API and in old browsers use popups?

  • @Gustavorodrigues I tested this API as described here and I couldn’t display notifications. Always ask for permission again. Also test how on this issue of SOEN and the result was the same. I would edit the question with this alternative, but for now nothing done. Who knows if I have free time do other tests. If you want to risk adding an answer with a functional example you will have mine upvote guaranteed.

  • The code of this question is for Webkit, I’m testing it here and it’s working. Jsfiddle

  • @Gustavorodrigues My Chrome doesn’t work. It must be because of Windows XP. Why am I using XP? Don’t even ask...

  • I also think notifications are a better way. That question right here at SOPT deals more with the subject and may have some useful examples.

  • 1

    @Luizvieira really, but it is good to consider that it does not work in ancient browsers and restricted environments (as mine). As I mentioned, notifications do not appear in Windows XP here from my work.

Show 2 more comments

2

var p = window.open("about:blank", "_blank", "width=610,height=610");
p.location = "http://www.google.com";

setInterval(function(){
    p.focus();
}, 60000);

Make the call from var p before anything else in your routine to prevent you from falling into the pop-up blocker.

  • 1

    In fact usually it just doesn’t fall into the blocker if the window.open is called by direct consequence of a user action: a click event, keydown or other.

  • @Gustavorodrigues exactly, so I prefer to always do this way. I wanted to emphasize the method location precisely for this reason!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.