Monday, June 2, 2008

How to call a javascript function in serverside code.

Today i was looking for a way to close the window in an C# webapplication. This can be done by using window.close(); in javascript. But how to call this from serverside code? So basically the question is how to run javascript from server side code.

It's really easy:


protected void btnUser_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "close", "window.close();", true);
}


You don't have to use the <script> tags when providing it with the last parameter set to true.

Apparently this wont work with firefox 2.0 and i cant seem to find a fix for this. For firefox <2.0 you can use the following:

window.open('','_parent','');
window.close();

Note: when using this in Internet Explorer it will ask you to close the window but if you add "window.open('','_parent','');" as above it wont!

Goodluck.

1 comment:

Offshore software development company said...

Thanks for sharing this article on Software development. It was very nice.

Looking for more..................Please continue.