.NET Framework Class Library
Controller.JavaScript Method
Creates a JavaScriptResult object.
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
Syntax
Visual Basic
Protected Friend Overridable Function JavaScript ( _ script As String _ ) As JavaScriptResult
C#
protected internal virtual JavaScriptResult JavaScript( string script )
Visual C++
protected public: virtual JavaScriptResult^ JavaScript( String^ script )
Parameters
- script
- Type: System.String
The JavaScript code to run on the client
Return Value
Type: System.Web.Mvc.JavaScriptResultThe JavaScriptResult object that writes the script to the response.
Remarks
This method is used to send JavaScript code that is created on the server to the client. The JavaScript is automatically run on the client. The result object that is prepared by this method is written to the response by the MVC framework when the object is executed.
Examples
The following example shows how to send jQuery code from the server to the client. The script runs immediately when the client receives it. When the "Test JavaScript" link is selected, the jQuery client script is sent to the browser and the browser displays "JavaScript Passed".
C#
<i><b>JavaScript Passed</b></i> will appear on the next line when you click the <b>Test JavaScript</b> link.<br /> <div id="divResultText"></div> <%= Ajax.ActionLink("Test JavaScript", "TestJavaScript", new AjaxOptions{UpdateTargetId = "divResultText"}) %>
C#
public ActionResult TestJavaScript() { string s = "$('#divResultText').html('JavaScript Passed');"; return JavaScript(s); }
Visual Basic
<i><b>JavaScript Passed</b></i> will appear on the next line when you click the <b>Test JavaScript</b> link.<br /> <div id="divResultText"></div> <%= Ajax.ActionLink("Test JavaScript", "TestJavaScript", new AjaxOptions{UpdateTargetId = "divResultText"}) %>
Visual Basic
Public Function TestJavaScript() As ActionResult Dim s As String = "$('#divResultText').html('JavaScript Passed');" Return JavaScript(s) End Function
See Also