fkr fkr - 1 month ago 6
C# Question

Javascript code to open pop-up window on button click?

I want to know how to write a javascript code that will open a new 600*300 pop-up window on button click.

Let's say i have a open-up button in my Books.aspx ...and when i click "display", i want a few books to be display in the pop-up window, which is another web-form called BooksAuthor.aspx

This is what i've tried so far:

Option 1

<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" onClick="window.open('BooksAuthor.aspx');" Text="Display" /><br /><br />


Option 2

<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" onClick="openWindow();"Text="Display" /><br /><br />

<script type="text/javascript">
function openWindow()
{
window.open("BooksAuthor.aspx", "status=1,width=600,height=300");
}


Nothing seems to be working. Any ideas?

Thank you!

Answer

you have to call javascript on onClientClick

<asp:Button ID="button1" runat="server" OnClientClick="window.open('BooksAuthor.aspx');" Text="Display" /><br /><br />

Or

<asp:Button ID="button1" runat="server" OnClientClick="openWindow();" Text="Display" /><br /><br />

<script type="text/javascript">
function openWindow() {
    window.open("BooksAuthor.aspx", "status=1,width=600,height=300");
}
Comments