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