2

In order to process special URLs in an already opened web application I use this approach:

  1. the user receives a special URL in an email (alarm notifications)
  2. the URL opens a small helper web page (H) with JavaScript code that temporarily sets a session cookie
  3. the main web application (M), which is already open in another tab/window, recognizes this and handles the request after deleting the cookie
  4. the helper web page (H) identifies this as a success and is now useless and should be closed.

This all works fine except for the helper window (H) remaining open. Of course there is a small text saying "please close this window now" but it would be perfect if it could do this automatically.

window.close() causes a confirmation dialog in IE and FireFox just ignores the command. I understand this is because the window has not been opened using window.open().

Similarly, calling window.focus() in the main window does not do anything, either.

Does anyone have an idea how to accomplish this anyway? At least, automatically focusing the main window without closing the helper window would be better than nothing.

Of couse I'm also open for other solutions to handle e-mail links in an already open web application... :)

Note the web application (and the helper page of course) are on a HTTPS server.

Udo G
  • 12,572
  • 13
  • 56
  • 89
  • possible duplicate of [How can I close a window with Javascript on Mozilla Firefox 3?](http://stackoverflow.com/questions/760422/how-can-i-close-a-window-with-javascript-on-mozilla-firefox-3) – epascarello Aug 23 '12 at 14:00
  • possible duplicate of [Close Window in Javascript](http://stackoverflow.com/questions/3670475/close-window-in-javascript) – JJJ Aug 23 '12 at 14:01
  • @espacarella, Juhana: not exactly a duplicate as I describe the reason behind it and perhaps someone has a different idea on how to solve it. I'll update the title.. – Udo G Aug 23 '12 at 14:06
  • *Why* you want to do it doesn't affect how to do it or whether or not you can do it. – JJJ Aug 23 '12 at 14:09
  • @Juhana: what I mean is that *perhaps* I can avoid to open a new window at all (like causing it to open in an `iframe` inside the main window..?) or somehow satisfy the browser's security requirements that will allow me to close the window.... So, it's not all about `window.close()`. – Udo G Aug 23 '12 at 14:14

1 Answers1

0

In some browsers, a window can only be silently closed through javascript if it was opened via javascript (for security purposes).

This code is a hack to get around this security measure:

// open a new window, replacing the current one
window.open('', '_self', '');
// close the current window, which was now technically opened via javascript
window.close();

Use at your own risk. These measures are in place to prevent you from doing annoying/malicious things to visitors of your page.

jbabey
  • 45,965
  • 12
  • 71
  • 94