0

I have web page on which there is a button 'start'. Now I want that onclick of that 'start' button this page will be closed. can i do this using javascript?

shubhendu
  • 213
  • 1
  • 3
  • 10

5 Answers5

2
<input type="button" value="Close" onclick="window.close()" />
user1844933
  • 3,296
  • 2
  • 25
  • 42
0

<script>window.close();</script>

Soundz
  • 1,308
  • 10
  • 17
0

Going to another page will close the current page (stopping execution of any other scripts on it, etc).

location = "http://duckduckgo.com";
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You will need Javascript to do this. Use window.close()

close(); Note: the current window is implied. This is equivalent:

window.close(); or you can specify a different window.

So:

function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}

with HTML:

<a href="javascript:close_window();">close</a>

or:

<a href="#" onclick="close_window();return false;">close</a>

You return false here to prevent the default behavior for the event. Otherwise the browser will attempt to go to that URL (which it obviously isn't).

Now the options on the window.confirm() dialog box will be OK and Cancel (not Yes and No). If you really want Yes and No you'll need to create some kind of modal Javascript dialog box.

Note: there is browser-specific differences with the above. If you opened the window with Javascript (via window.open()) then you are allowed to close the window with javascript. Firefox disallows you from closing other windows. I believe IE will ask the user for confirmation. Other browsers may vary.

Kaushik
  • 2,072
  • 1
  • 23
  • 31
0

You can only use Javascript to close a window that was created via javascript, this is a security feature. Therefore window.close(); is not going to work in most scenarios as some other users have pointed out.

What I'd do is open an new instance of the current window setting the target to _self and then closing this e.g:

window.open(window.location, '_self').close();

Or:

<script type="text/javascript">
     function closeWindow(){
        window.open(window.location, '_self').close();
     }
</script>

<input type="button" id="btnClose" value="Close window" onclick="closeWindow();" />

See the w3.org docs on window.close(). Pay close attention to:

The close() method on Window objects should, if all the following conditions are met, close the browsing context A:

  • The corresponding browsing context A is script-closable.
  • The responsible browsing context specified by the incumbent settings object is familiar with the browsing context A.
  • The responsible browsing context specified by the incumbent settings object is allowed to navigate the browsing context A.

In particular:

The corresponding browsing context A is script-closable.

What is "script closable"?

A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.

The proposed solution is a bit of a hack though. If compatibility is an issue I would recommend doing whatever it is you're doing in this window inside a pop-up/'script closable' window so that you can guarantee support.

Community
  • 1
  • 1
DGibbs
  • 14,316
  • 7
  • 44
  • 83