39

How to close a window using jQuery for all browser compatibility?

<input type="button" 
       name="backButton" 
       value="Close" 
       style="background-color:#245f91; color:#fff;font-weight:bold;" 
       onclick="window.close();">

This onclick event is working in IE. Could you help me to write code in jQuery?

1
  • 4
    window.close is plain javascript. Nothing in jQuery will make it more cross browser. It is the browser itself that decides if it allows your script to close the window. Commented Jan 17, 2012 at 6:15

5 Answers 5

62
$(element).click(function(){
    window.close();
});

Note: you can not close any window that you didn't opened with window.open. Directly invoking window.close() will ask user with a dialogue box.

Sign up to request clarification or add additional context in comments.

Comments

17

just window.close() is OK, why should write in jQuery?

Comments

17

You can only use the window.close function when you have opened the window using window.open(), so I use the following function:

function close_window(url){
    var newWindow = window.open('', '_self', ''); //open the current window
    window.close(url);
 }

3 Comments

I think that should be "newWindow.close(url);". But in any case, this worked great and should be the accepted answer!
Good answer. Yes, it should be "newWindow.close(url);". Why are you using the url parameter?
Sadly does not work in Edge. Works current Chrome (but Chrome seems to let any old link close a window) and works with IE 11.... Bugger.
4

For IE: window.close(); and self.close(); should work fine.

If you want just open the IE browser and type

javascript:self.close() and hit enter, it should ask you for a prompt.

Note: this method doesn't work for Chrome or Firefox.

Comments

1

This will only work for windows which are opened by using window.open(); method. Try this

var tmp=window.open(params);
tmp.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.