0

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 :

http://server/solutionName/Confirmation.aspx

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: enter image description here

Community
  • 1
  • 1
Abdul
  • 2,002
  • 7
  • 31
  • 65

3 Answers3

1

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();
        }
Ishfaq Ahmad
  • 88
  • 1
  • 13
0

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>
Alex Vazhev
  • 1,363
  • 1
  • 18
  • 17
0

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

Vijay
  • 2,965
  • 1
  • 15
  • 24
  • then how can i close an aspx page on a button ? – Abdul Jan 27 '15 at 09:32
  • If you want to redirect user to close the current page and open another one....tell the server. :) or, you can open the child window from script and then close it with script :) – Vijay Jan 27 '15 at 09:34