0

What I'm looking to do is create a new window using window.open and then when the new window opens, I want to check if that window is active throughout?

For example: I have

myNewWindow = window.open(document.getElementById("inputbox").value).focus();

Once this window opens, I want to increment the value of variable increase if myNewWindow loses focus.

I'm doing :

if(myNewWindow.onblur()){
    increment value...
}
setTimeout('myNewWindow.close()',3000); // closing window after 3 seconds. So, I'm really trying to check if the window was in focus for those 3 seconds.

However, the onblur does not seem to work no matter what I try. but, window does close. Any ideas anyone? I just need to find out if the new opened window was minimized or some out of focus.

Its fairly easy to detect if the window in which you are running your JS is losing focus or not. And the same does not seem to work in my case. For example: JavaScript / jQuery: Test if window has focus

Community
  • 1
  • 1
  • For safety reasons, these things are pretty hard. I believe there is a Socket-like way to do this. – 11684 Oct 08 '12 at 06:16
  • **Never** pass a string to `setInterval()` or `setTimeout()`. Doing so is as bad as using `eval()` and it results in unreadable and possibly insecure code as soon as you use variables since you need to insert them into the string instead of passing the actual variable. The proper solution is `setInterval(function() { /* your code *) }, msecs);`. The same applies to `setTimeout()`. If you just want to call a single function without any arguments, you can also pass the function name directly: `setInterval(someFunction, msecs);` (note that there are **no** `()` behind the function name) – ThiefMaster Oct 08 '12 at 06:19
  • So, can it not be done at all? Because I can almost do everything else with the new window, like adjust the size, close it after an interval, etc. But, checking if the window was active throughout doesn't seem to work. – Watchful Protector Oct 08 '12 at 06:19

1 Answers1

2

From page1, no you cannot directly set or subscribe to events on page2. However, you can use something like window.postMessage to pass messages between them.

Geuis
  • 41,122
  • 56
  • 157
  • 219
  • So you mean I am trying find a solution that does not exist at all?! :O Yeah...I figured you can communicate with page 2 from page 1, but I haven't been able to find anything on the problem i'm trying to solve. – Watchful Protector Oct 08 '12 at 06:24
  • A solution is possible, just in a different way. You would need some js on page2 that handles 'message' events coming from page1 and communcates back to page1. One idea is to add an event listener for 'blur' to the window object on page2. When blur happens, i.e. page2 loses focus, it can fire a postMessage to page1. – Geuis Oct 08 '12 at 18:41
  • How would I add js for page2, when I am working with just 1 js file and that is the one which is responsible for taking the URL to open from the user and then closing the window after a certain no of seconds?? I'm a newbie and so, kindly excuse me if this question is mediocre. – Watchful Protector Oct 08 '12 at 19:01
  • 1
    Not a mediocre question at all. This *only* works on the assumption that you have control over the document being loaded into page2. If page2 is a 3rd party website, you can't insert javascript into it. – Geuis Oct 08 '12 at 19:18
  • The page2 (being opened by window.open on page1) HAS TO BE A URL that the user enters in the form on page1. So, 9/10 times it's going to be a site which I have no control over. To be clearer, I have to open page2 from page1. And page 2 should close ONLY IF any other browser or window was opened. I did post earlier asking if it was possible to detect if a new window or browser has been opened and was mocked for it. So, I thought, maybe checking with onblur and focus might help, IF THAT MAKES SENSE. – Watchful Protector Oct 08 '12 at 19:26