1

My application sends an e-mail to the manager with an approve and a reject button

  • When the manager clicks the button, the web page will open.
  • In that page is a close button. On click of close button it should close the browser window. But it is not closing and it throws an error message on console "Scripts may not close windows that were not opened by script."

I tried to close the window, i.e, already open. The fallowing ways

var win = window.open('','_self');
win.close();

window.top.opener=null;
window.close();

var win=window.open("","_self");
win.close(); 

window.open('','_parent',''); 
window.close();

The above did not work. Can someone provide a solution for this.

Thank you in advance. Please don't close this. Because i googled lot, I did not find solution.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
  • does the console throw an error? – JNF Jun 09 '15 at 06:29
  • yes. "Scripts may not close windows that were not opened by script." – user3747449_Santhosh Jun 09 '15 at 06:34
  • Actually what happens is. In my application i will send a mail to manager to approve or reject leave. with approve and reject button. when the manager clicks the button, the web page will open, in that page there a close button. On click of close button it should close the browser window, But it is not closing. and it throws error message on console like this "Scripts may not close windows that were not opened by script.". – user3747449_Santhosh Jun 09 '15 at 06:37
  • 2
    If your script did not initiate opening the window (with something like window.open), then the script in that window is not allowed to close it. Its a security to prevent a website taking control of your browser and closing windows.The console error message is clear huhh? – AkshayJ Jun 09 '15 at 06:39
  • In what application does the manager get the e-mail (browser, outlook, thunderbird, ...)? – surfmuggle Jun 09 '15 at 06:57
  • Hi, threeFourOneSixOneThree, the manager get the e-mail in any one of the these browser, outlook, thunderbird, etc., – user3747449_Santhosh Jun 09 '15 at 07:06

2 Answers2

3

See MDN on window.close():

This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

JNF
  • 3,696
  • 3
  • 31
  • 64
1

I would use jQuery UI component Dialog.

 $(function() {
    $( "#dialog" ).dialog();
  });
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div id="dialog" title="Basic dialog">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

You get that error message because outlook redirects link clicks to a new browser instance. After it is opened he has no control over it.

You can close pages that it opens, but you definitely can not close browser instance itself.


If you send it via email then depending of mail viewer you have limited support. Ex: gmail strips away html head, body, all non inline css and javascript. In short : jQuery UI Dialog would not work.

Instead use a link with get parameters (ex: unique user id) and read it when page is opened.

Margus
  • 19,694
  • 14
  • 55
  • 103
  • Does this work even if the window was opened from within an e-mail client (for example outlook)? – surfmuggle Jun 09 '15 at 06:49
  • Outlook uses MS Word rendering engine and not even IE. It is even more basic and it's limited even more, i doubt it. If possible I would go with dialog as http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome states, there are only hack solutions around it, that would introduce security bugs when created. – Margus Jun 09 '15 at 06:54
  • This doesn't answer the question – Royi Namir Jun 09 '15 at 08:07