JavaScriptResult Class
The JavaScriptResul class implements the abstract ActionResult class and is used to sends JavaScript content to the response.
Example:
1)
Somewhere in Home controller:
public JavaScriptResult RunJS() {
var script = "alert('Javascript is running');";
return new JavaScriptResult() { Script = script };
}
and somewhere in a some view:
<script>
$(document).ready(function () {
$("button").click(function () {
$.getScript("/Home/RunJS");
});
});
</script>
<button>Use the button to run a JavaScript</button>
2)
Somewhere in Home controller:
public JavaScriptResult RunJS() { var script = string.Format(@"numberOfProducts = 135;",; return new JavaScriptResult() { Script = script }; }
and somewhere in a some view:
<script type="text/javascript" src="/Home/RunJS">
if(numberOfProducts >= 100)
{
alert('Javascript is running'); }
</script>