closing main window in firefox

Discussion in 'JavaScript and AJAX' started by msullivan3, Jun 23, 2009.

  1. msullivan3

    msullivan3 New Member

    Joined:
    Apr 27, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    i am running the following script:

    Code:
        <script language="javascript" type="text/javascript"> 
          function closeWindow() { 
          window.open('','_parent',''); 
          window.close(); 
        } 
        </script>
    
    i am trying to close the window and it works for all browsers except firefox. i am not trying to close a popup window, but rather the main window. is there a better method that closes for all browsers?
     
    Last edited by a moderator: Jun 23, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Try this :

    Code:
    function closeWindow() {
    newwin = window.open(location.href,'_parent','');
    newwin.close();
    }
     
  3. msullivan3

    msullivan3 New Member

    Joined:
    Apr 27, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    i just tried what you said, but it still isnt working.
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    OK. I read this in the mozilla forum :

    You can use window.open() to open a new window and then, try to close it by the script; which will hopefully work on all browsers.
     
  5. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    However, in some browsers it is not allowed to automatically close windows that have not been opened by JavaScript. The line above works fine in Explorer on Mac, Mozilla, Opera and OmniWeb, but not in Explorer on Windows, Netscape 4 and lower and iCab. In these browsers the user is asked to confirm the closing of the window. As to Safari, it does absolutely nothing.
    Rather to my surprise it's very easy to get around this confirm box in Explorer 5.5 and 6 on Windows. Explorer merely looks if the page has an opener. If it doesn't the window has been opened by the user and may not be closed without a confirm. So what we need to do is trick Explorer into thinking the opening page has an opener:

    Code:
    opener.opener = top; // or whatever, as long as opener.opener has a value;
    opener.close()
    
    This trick doesn't work in Netscape 4 and lower and iCab, these browsers have more sophisticated ways to determine whether a window has been opened by JavaScript.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice