-1

I want to create a link that says 'Close' and it should close the window. Anyone have any idea how to do this?

I've looked at other people's questions and it seems I can't find anything that works.

This is what I have right now:

<a id="noThanks" href="JavaScript:window.close()">No, thanks</a>
user3767997
  • 83
  • 3
  • 10
  • Just to clarify, by popup do you mean an element that is in the same window as the site content, only higher? Closing such pop up is done by hiding that element with `display: none` CSS rule. – Narxx Jul 09 '14 at 17:01
  • Popup is the wrong term, I should have just said window. – user3767997 Jul 09 '14 at 17:03

5 Answers5

0

There is javascript command for closing current window.

window.close();

or if you are opening named window

function openWin() {
    myWindow = window.open("http://www.domain.com", "_blank", "width=200, height=100");
}

function closeWin() {
    myWindow.close();
}
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • I guess I'm doing it incorrectly because I've tried that and I can't get it to work. Can you give me an example, please? – user3767997 Jul 09 '14 at 17:04
  • 1
    @user3767997 [*This method is only allowed to be called for windows that were opened by a script using the window.open method.*](https://developer.mozilla.org/en-US/docs/Web/API/Window.close#Description) – George Jul 09 '14 at 17:05
  • @oGeez there's an example right below to close current widow. BTW how did you do the link in comment? – Matas Vaitkevicius Jul 09 '14 at 17:07
  • Is there any other way to do it? Sorry if I'm asking dumb questions, I'm still a beginner. – user3767997 Jul 09 '14 at 17:07
  • @LIUFA Yes, a window that is first opened using JavaScript. See http://stackoverflow.com/editing-help#comment-formatting – George Jul 09 '14 at 17:10
  • What is the purpose of this? If you open a window, and your focused on that window, you could exit that window by simply clicking on the browser's X at the top. If you're trying to close the new opened window from within the old window, you might be able to do that with what @LIUFA wrote, but why would you want to close a window from within another? I'm not quite clear on what you are trying to accomplish... :/ – Narxx Jul 09 '14 at 17:12
  • @Narxx okay so I'm creating a submission form that the user has the option to opt out of by clicking 'No, thanks'. The job of this link is to close the submission form. Let me know if that doesn't answer your question properly. – user3767997 Jul 09 '14 at 17:15
  • Okay, so why don't you actually create a 'popup' element within your window, that will be displayed over the other elements, with a small X button or 'close' link on top? Most apps work that way and it's very intuitive for users to work with. Creating new windows is annoying and you only open new windows when you want to go elsewhere without losing the context of where you came from. In your case, I would make a popup, darken a bit the background and let the user choose whether to fill or close the form. – Narxx Jul 09 '14 at 17:27
  • @Narxx That sounds like a good idea and I didn't think about that. I'll look into that. Thanks! – user3767997 Jul 09 '14 at 18:41
0

Using win.close() will close the window if win is a variable that has the value of a window.open() call stored in it.

So,

var win = window.open("//stackoverflow.com");
win.close();

will close the window opened by window.open().

However, window.close() only works on a window that was opened by JavaScript. That is, you can't close a window that a user navigated to by clicking a link.

To control a window, you'd have this:

<a href="javascript:var win = window.open('//link.to/new_page');">Open Window</a>

Then, in your new window, you'd have this to close it:

<button onclick="win.close();">Close</button>
AstroCB
  • 12,337
  • 20
  • 57
  • 73
0

You can only close a tab/window that you've opened using window.open

Similar question and answers can be found here

Community
  • 1
  • 1
budiDino
  • 13,044
  • 8
  • 95
  • 91
0

This is the answer I was really looking for in case anyone else ever needs it. Just a heads up--this doesn't work in FF.

window.open('', '_self');
window.close();
user3767997
  • 83
  • 3
  • 10
0

In my case once data saved I need close window after confirmation .

<script type="text/javascript">
if (confirm("Close Window?"))
  {
   close();
  }
  </script>

Don't forgot to call jquery library before your script

Ganesan J
  • 539
  • 7
  • 12