locked
window.close() not working in deployed website RRS feed

  • Question

  • User966655012 posted

    If a button is clicked, the current window should get closed.

    I have used following methods.

    1. window.close();
    2. open(location, '_self').close();
    3. var win = window.open('', '_self');
      win.close();
    4. var win = window.open("about:blank", "_self");
      win.close();

    All these are working well while debugging. But after deployment, it is not working.

    Please help me with this.

    Wednesday, April 11, 2018 10:25 AM

All replies

  • User516094431 posted

    Try following line of code. It's working for me. 

    <input type="button" id="btnClose" onclick="javascript:var win = window.open('', '_self');win.close();return false;" value="Close" />

    Wednesday, April 11, 2018 11:55 AM
  • User966655012 posted

    Even this didn't work.

    I tried the below code.

    $(document).on("click", "a.CloseWindow", function () {
    func_CloseWindow();
    });

    function func_CloseWindow() {
    var win = window.open('', '_self');

    win.close();
    return false;
    }

    It is working but not always. Sometimes window get closed and sometimes not.

    When I clear history, it is working. Not working otherwise.

    Wednesday, April 11, 2018 2:50 PM
  • User753101303 posted

    Hi,

    You may have tricks to do that in some browsers but AFAIK the behavior you see is expected. You are supposed to be able to close a window if you opened the window programmatically or if it doesn't have any history (as usual likely for safety reasons and to keep the user in control).

    Wednesday, April 11, 2018 3:03 PM
  • User2053451246 posted

    How are you opening the window that you are trying to close?  If you didn't launch the window by user interaction from your public website window.close won't always work.

    More information: https://stackoverflow.com/a/19768082

    Wednesday, April 11, 2018 3:05 PM
  • User966655012 posted

    I am a fresher and working in Tech Mahindra.

    This is my first developement project and I am working all alone.

    The requirement is, if user clicks logout(anchor tag) window should get closed.

    While debugging from visual studio, window is getting closed perfectly in IE and Chrome.

    After deploying, while accessing through the link, say http://abc.com

    window is getting closed in IE version11. In chrome it is getting closed sometimes and sometimes not. If I clear history, window is getting closed.

    I tried many codes mentioned in my post, but didn't work in chrome. Finally I tried with the below code and it works, but not always.

    $(document).on("click", "a.CloseWindow", function () { 
    func_CloseWindow();
    });

    function func_CloseWindow() { 
    var win = window.open('', '_self');

    win.close();
    return false;
    }

    Thursday, April 12, 2018 6:18 AM
  • User-330142929 posted

    Hi

    try this.

    <a href="javascript:self.close()" >Close</a>

    Or.

    function CloseWebPage(){
     if (navigator.userAgent.indexOf("MSIE") > 0) {
      if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
       window.opener = null;
       window.close();
      } else {
       window.open('', '_top');
       window.top.close();
      }
     }
     else if (navigator.userAgent.indexOf("Firefox") > 0) {
      window.location.href = 'about:blank ';
     } else {
      window.opener = null;
      window.open('', '_self', '');
      window.close();
     }
    } 

    look forward to your reply

    best regards

    Abraham

    Thursday, April 12, 2018 6:39 AM
  • User966655012 posted

    Issue is with chrome. So, code for chrome please:)

    Thursday, April 12, 2018 6:44 AM
  • User753101303 posted

    Make sure the difference in behaviour you see doesn't depend on the history.

    In short a user window is supposed to be closed by the user. Rather than trying to go against how the web work, my first step would be to see which benefit we are trying to get by having a closing link in addition to the browser UI.

    Thursday, April 12, 2018 6:52 AM
  • User966655012 posted

    If cache is cleared, the below code is working. How can I clear the cache on closing the window.

    $(document).on("click", "a.CloseWindow", function () { 
    func_CloseWindow();
    });

    function func_CloseWindow() { 
    var win = window.open('', '_self');

    win.close();
    return false;
    }

    Thursday, April 12, 2018 6:53 AM
  • User753101303 posted

    You can't clear the browser page cache for the same reason. A browser is a sandbox and you have little control on how the surrounding environment works for safety reason and to keep the user in control.

    Now one could think about not caching pages at all. Now is this just so that a user can click on a close link rather than to use the close button already shown by the browser ?

    As I said earlier I prefer to embrace how the web works rather than trying to go against its principle, possibly talking with those making requirements not well suited to the web.

    At worst a common approach is to show a message and try to close the window. It will work if possible and else the user can see the message and close himself the window.

    Thursday, April 12, 2018 7:40 AM
  • User-330142929 posted

    Hi,

    Issue is with chrome. So, code for chrome please:)

    else {   //if browse is Chrome ,handle it with this 
       window.opener = null;
       window.open('', '_self', '');
       window.close();
      }

    This Code works fine in my Chrome Browse. What's the Problem , Please?

    look forward to your reply .

    Best Regards

    Abraham

    Friday, April 13, 2018 2:38 AM
  • User-1823510884 posted

    You can use Following JavaScript Functions to Open and close a window .

    // Opening a Window

    function openWindow() {
        newWindow = window.open("", "Window Title", "width=200, height=100");   // This Opens a new window
    }

    //Closing a Window
    function closeWin() {
        newWindow.close();   // Closes the new window
    }

    Usage:

    <button onclick="openWindow()">Open Window</button>

    <button onclick="closeWin()">Close Window</button>

    Friday, April 13, 2018 4:59 AM