javascript close current window without prompt

I searched a lots of solution for closing window without prompt and I got lots of script, but scripts was  not working for all the version of  internet explore some working in IE7 and some in IE8 so I wrote an unique solution which works in all version of internet explore.

When we tries to close a window (the window that is not opened through the JavaScript) using JavaScript window.close() method in IE, IE throw a prompt "The Webpage you are viewing is trying to close the window. Do you want to close this window?"

Because of the security enhancements in IE, we can't close a window unless it is opened by a script. So the walk around will be to let the browser thinks that this page is opened using a script then closing the window. Below is the implementation.




<script type="text/javascript">
function closeWP() {
 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>

Popular Posts