27

I have an html button that I want to close the current window when clicked. I thought I could just set the onclick feature like I did below. Am I missing something?

<button type="button" onclick="javascript:window.close()">Discard</button>
jcmitch
  • 2,088
  • 9
  • 27
  • 33

7 Answers7

49

When in the onclick attribute you do not need to specify that it is Javascript.

<button type="button" 
        onclick="window.open('', '_self', ''); window.close();">Discard</button>

This should do it. In order to close it your page needs to be opened by the script, hence the window.open. Here is an article explaining this in detail:

Click Here

If all else fails, you should also add a message asking the user to manually close the window, as there is no cross-browser solution for this, especially with older browsers such as IE 8.

amosrivera
  • 26,114
  • 9
  • 67
  • 76
  • 3
    Thanks that is good advice but the actual problem is that I'm a moron. Wasted tons of time because I was testing a different file from the one I was editing. Wow... Thanks for the good advice though! – jcmitch Nov 08 '11 at 22:17
  • nice... I'll have to use this next time I have to write a script that creates 130 new windows – Joseph Marikle Nov 08 '11 at 22:19
  • Well that sure seems to circumvent the entire point of not allowing a raw `window.close()` call... – Dustin Graham Oct 14 '13 at 18:18
  • Doesn't work for me in Chrome 72.0.3626.121, 64-bit. Also, to note, the linked article uses _parent instead of _self. But neither worked per my testing on a local development page -- when the page was opened directly in Chrome, not via script or link from another page. However, works in Edge! :) But `window.close()` alone also works -- with a confirmation. – GG2 Mar 14 '19 at 19:15
  • 3
    'but the actual problem is that I'm a moron.' - hilarious - we have all been there brother! – Jason Is My Name Apr 09 '19 at 14:21
  • Does not work and in console see `Scripts may close only the windows that were opened by it.` – user2360831 Mar 17 '20 at 08:18
  • As at 2021-10-06, does not work in Chrome 94.0.4606.71, Edge 94.0.992.38, Firefox 92.0.1. Looks like they got wise to the subterfuge. – Patanjali Oct 06 '21 at 06:20
19

JavaScript can only close a window that was opened using JavaScript. Example below:

<script>
function myFunction() {
  var str = "Sample";
  var result = str.link("https://sample.com");
  document.getElementById("demo").innerHTML = result;
}
</script>
rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
13

Use the code below. It works every time.

<button onclick="self.close()">Close</button>

It works every time in Chrome and also works on Firefox.

Anbuselvan Rocky
  • 606
  • 6
  • 22
Anonymous
  • 169
  • 2
  • 6
  • 3
    Anecdotal evidence and personal assurances don't make for a better answer. Besides, this script will be prevented from running wherever the page wasn't opened by another script. – axolotl Jul 18 '18 at 15:55
4

Closing a window that was opened by the user through JavaScript is considered to be a security risk, thus not all browsers will allow this (which is why all solutions are hacks/workarounds). Browsers that are steadily maintained remove these types of hacks, and solutions that work one day may be broken the next.

This was addressed here by rvighne in a similar question on the subject.

Ozgar
  • 305
  • 2
  • 14
3

This site: http://www.computerhope.com/issues/ch000178.htm answers it with the script below

< input type="button" value="Close this window" onclick="self.close()">

KenP
  • 31
  • 3
2

I know this thread has been answered, but another solution that may be useful for some, particularly to those with multiple pages where they want to have this button, is to give the input an id and place the code in a JavaScript file. You can then place the code for the button on multiple pages, taking up less space in your code.

For the button:

    <input type="button" id="cancel_edit" value="Cancel"></input>

in the JavaScript file:

    $("#cancel_edit").click(function(){
        window.open('','_parent',''); 
        window.close(); 
    });
Brian T
  • 21
  • 6
2

November 2019: onclick="self.close()" still works in Chrome while Edge gives a warning that must be confirmed before it will close.

On the other hand the solution onclick="window.open('', '_self', ''); window.close();" works in both.

LesD
  • 35
  • 6
  • 3
    `Scripts may close only the windows that were opened by them.` is what I get doing either of those. In Chrome console. – Atom999 Jul 31 '20 at 21:31