Reputation: 2120
I have searched this issue on other links like
window.close and self.close do not close the window in Chrome etc. but couldn't able to solve it
I want to close a page using below code, but it's not working on any browser.
my url is like :
Warning :
Scripts may close only the windows that were opened by it.
HTML:
<img alt="Close" src="images/close.png" onclick="return confirm_delete();">
Script:
function confirm_delete()
{
if (confirm("Are you sure you want to close?") == true)
window.close();
else
return false;
}
Here is the screenshot:
Upvotes: 0
Views: 2089
Reputation: 88
Try the following lines of code
var Browser = navigator.appName;
var indexB = Browser.indexOf('Explorer');
if (indexB > 0) {
var indexV = navigator.userAgent.indexOf('MSIE') + 5;
var Version = navigator.userAgent.substring(indexV, indexV + 1);
if (Version >= 7) {
window.open('', '_self', '');
window.close();
}
else if (Version == 6) {
window.opener = null;
window.close();
}
else {
window.opener = '';
window.close();
}
}
else {
window.close();
}
Upvotes: 1
Reputation: 1461
The hacky workarounds for Chrome (reopen window inside itself and close it):
<script>
open(location, '_self').close();
</script>
This code works in my Chrome:
<script>
function confirm_delete()
{
if (confirm("Are you sure you want to close?") == true) open(location, '_self').close();
else return false;
}
confirm_delete();
</script>
Upvotes: 0
Reputation: 3023
window.close
When this method is called, the referenced window is closed.This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.
https://developer.mozilla.org/en-US/docs/Web/API/Window.close
Upvotes: 0