How to call javascript function from server side
-
1Please add some more detail to this question, I doubt whether it can be answered in this hazy state. Do you want to execute JavaScript code on server side? Or do you want to access a JavaScript function in the user's browser?Pekka– Pekka2010-07-03 10:48:14 +00:00Commented Jul 3, 2010 at 10:48
Add a comment
|
2 Answers
You can simply do a Response.Write("<script language='javascript'>nameOfJavascriptFunction();</script>"), or use ClientScriptManager.RegisterClientScriptBlock with a similar script.
The effect is that you are outputting a script tag calling your function - the call is still done on the client side.
2 Comments
Dylan - INNO Software
Correct me if I'm wrong, but won't you get this error trying that code? Non-invocable member 'System.Web.HttpResponse.Output' cannot be used like a method. Also, wouldn't we need to escape those double-quotes in the 'language' attribute? Perhaps since you wrote this it has changed? This might be the way to go now: Response.Write("<script language=\"javascript\">nameOfJavascriptFunction();</script>"); @Oded - I'll vote up for the right answer, I just wanted to make sure it is current.
Oded
@Dylan-INNOSoftware - Naw, you got it right... Thanks - good catch :)