While I was working on some HTML files which requires closing the main window when I clicked on the close button which my application provided. I used window.close() javascript to close the browser window. It works perfectly fine under Internet Explorer 6 or lesser versions (<=IE6).

However the problem comes when I tested it on the IE7 and Netscape 7.0 and Netscape 7.1. IE7 gives me a prompt which is asking whether to close the window or not. The script is not at all functioning in the Netscape browser.

After a lot of research I came to know that the problem is that we are closing the window using the script which was not opened by the script. So I made changes to the code which opens some new file in the same window using the javascript.

The new file contains the window.close() method in its onload() method of BODY tag.So the code looks like this..

function closeWindow(){
var agt=navigator.userAgent.toLowerCase();
var pos = agt.indexOf('msie');
version = navigator.userAgent.substring(pos + 5).substring(0, 1);
if((agt.indexOf("msie") != -1) && (version 7)
top.opener=top;top.close();
}else{
window.top.open("w2Close.html","_self");
}
}

This will works fine for the Internet Explorer v6 and above and Netscape v7 and above. The idea here is the browsers IE7 and Netscape support tabbed browsing so we cannot close them using window.close(). So we close the browser by opening a new file in the same window using script.


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet