Tuesday, October 29, 2013

window.close in Firefox

In our programming and scripting world we many times come across requirements that do not find a way out, and are finally pushed in a category of Issues that cannot be resolved.

Similar thing came to me this morning when i had to close a browser tab on clicking a link on the page.

No big deal ;)

Within few minutes i scripted something as under:

 <script language="javascript" type="text/javascript">  
 function closeWindow()  
 {  
   window.close();  
 }  
 </script>  

and then i called the JavaScript function in my page's html. All worked well.

It all started when i accidentally checked my page in Mozilla Firefox. With all tweaks and all irrelevant script code written there was no way out.

*Some scripts i tried by searching on web. all commented script is what i tried. Nothing happenned.

 <script language="javascript" type="text/javascript">
 function closeWindow()  
 {  
   /* 1  
   window.open('','_parent','');  
   window.close();  
   window.open('','_self','');  
   window.close();  
   */  
   /* 2  
   window.addEventListener("beforeunload", function (e) {  
   var confirmationMessage = "\o/";  
   (e || window.event).returnValue = confirmationMessage;     
   return ConfirmationMessage;  
   });  
   */  
     /* 3  
     this.focus();  
   self.opener=this;  
   self.close();  
    */   
   /* 4  
   window.open('javascript:window.open("", "_self","");window.close();', '_self');  
   */  
   /*  
   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();  
   }  
   */  
 }
 </script>  
 

Then i came across few things that i had ignored, like window.close works only for browser windows that were opened using script, which means that you can close a browser window using window.close only if you had opened it using window.open. But nevertheless IE (Internet Explorer) is an exception.

I am not sure whether this is a loophole or is a functionality provided which any other browser does not have. In IE you can close a browser window using window.close even if it is not opened using window.open.

While searching i came across the following:
 dom.allow_scripts_to_close_windows  

This is a FireFox pref in about:config file of FireFox, by default its default value is false and it does not allow to close a browser window using window.close if it is not opened using window.open.

If this value is changed to true, then it works fine. But i could not find a way to change it programmatically and more over no one would want an application to change his/her system's application settings.

On your local environment you can change this value to true and check. It works. You can access the pref as mentioned:

 1) Type about:config in the Firefox address bar  
 2) Look for dom.allow_scripts_to_close_windows [default value is set to false]  
 3) Double click this pref to set it to true.  

This would allow the window.close for browser window not opened using window.open.

So this was some research i did and there was no option i could find to close the browser window in FireFox. If you guys have come across similar situation and were able to solve it, i request you people to please share the valuable information.

Regards
Vivek Joshi

No comments: