JavaScript code to close one page from another page in an Asp.net Project and compatible in all browsers.

cross-browser

JavaScript code to close one page from another page in an Asp.net Project and compatible in all browsers.

In one of my current projects I had to close a window from another another web page of my project.

Now this can be easily achieved by this simple code

onclick="javaScript:window.close(); return false;"

However I found that this code is not compatible to all browsers.

So here is some code which I found which works in all browsers

//Inline code
<script language="javascript" type="text/javascript">
var redirectTimerId = 0;
function closeWindow() {
window.opener = top;
redirectTimerId = window.setTimeout('redirect()', 2000);
window.close();
}

function stopRedirect() {
window.clearTimeout(redirectTimerId);
}

function redirect() {
window.location = ‘default.aspx’;
}
</script>

// Code Behind

protected void btnClose_Click(object sender, ImageClickEventArgs e)
{
ClientScript.RegisterStartupScript(typeof(Page), “closePage”, “window.open(‘javaScript:window.close()’, ‘_self’, null);”, true);

}