简体   繁体   中英

How do I close a window in JavaScript/jQuery?

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>

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();
}

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>

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

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM