Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Button Close Window

Tags:

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> 
like image 812
jcmitch Avatar asked Nov 08 '11 22:11

jcmitch


People also ask

How do I close a window in HTML?

The close() method closes a window.

How do I create a close button in HTML?

A close button is a <button> element with the class . close-button . We use the multiplication symbol ( &times; ) 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.

How do I close a window button?

Press Alt + F4 to close a window.

What button was clicked 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.


1 Answers

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.

like image 85
amosrivera Avatar answered Oct 19 '22 10:10

amosrivera