how to send js library to client dynamically?

147 Views Asked by At

I have a string variable that contains some JavaScript codes.

I want to send these codes to client as a JavaScript file attached! just like:

<script type="text/javascript" src="/lib/some.js"></script>

but this some.js file is not exist on the server!

how can I do this in ASP.net MVC3?

2

There are 2 best solutions below

0
On

You can add your string variable to your model ("MyJavascriptCode"), then simply output the contents inside a script tag on your view. Html.Raw outputs content with encoding it.

<script type="text/javascript">
     @Html.Raw(Model.MyJavascriptCode)
</script>
0
On

To call from a tag script, you can create an action that returns a JavaScriptResult

public JavaScriptResult GetSomeScript()
{
      var script = LoadFromSomeWhere();
      return JavaScript(script);
}

Check this out: http://forums.asp.net/t/1716356.aspx