How to close a window using jQuery for all browser compatibility?
$(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.
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.
This will only work for windows which are opened by using window.open(); method. Try this
var tmp=window.open(params);
tmp.close();
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);
}
just window.close()
is OK, why should write in jQuery?