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>
The close() method closes a window.
A close button is a <button> element with the class . close-button . We use the multiplication symbol ( × ) as the X icon. This icon is wrapped in a <span> with the attribute aria-hidden="true" , so screen readers don't read the X icon.
Press Alt + F4 to close a window.
(eXit button) Also called a "close" or "exit" button, clicking or tapping the X removes the current window, dialog box or popup message from the screen. It is also used to delete text and graphics.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With