Thursday, December 6, 2012

Open and Close Pop-UP window in JavaScript


Add the below code in the web page header tags.

 <script language="javascript" type="text/javascript">

     function pop_up(url) {
         newwindow = window.open(url, 'name', 'height=517,width=885,scrollbars=yes,toolbar=no,menubar=no,resizable=yes,location=no,directories=no,status=no,titlebar=no,left=400,top=120');
         if (window.focus) { newwindow.focus() }
         return false;
     }
   
</script>

Now we need call pop_up() function on a button click and pass the URL of the page to be open as a POP-UP. Here i'm using a ASP.NET link button.

<asp:LinkButton ID="lblPupUP" runat="server" OnClientClick="return pop_up('About.aspx')">

    Open PoP-UP</asp:LinkButton>

You can use below code for closing the pop up window.

<script language='javascript'>

        function closeWindow() {
            window.close();
        }
    </script>

  <asp:Button ID="Button1" runat="server" Text="Close" OnClientClick="closeWindow()" />

If you want to execute some server side code and then close the popup use below code on "Close" button click event.

protected void Button1_Click(object sender, EventArgs e)
    {
       // Server code goes here
        string strString1 = "<script language='javascript'>window.close();</script>";
        RegisterStartupScript("close", strString1);
    }

If you are using a ajax script manager on your page then use the below code.

protected void Button1_Click(object sender, EventArgs e)
    {
         // Server code goes here
        string strString1 = "<script language='javascript'>window.close();</script>";
        ScriptManager.RegisterStartupScript(Button1, Button1.GetType(), "close", strString1, false);
   
    }

No comments:
Write comments
Recommended Posts × +