What's the best way to embed server side persistent tags into javascript code?

I have a bunch of javascript functions that depend on some server-side constants (like lines from my resource file). Although I was developing them, I kept the javascript code in the view header, so I could just use server side tags in my javascript code, but now I would like to move the javascript functions into a separate file.

I cannot use a regular js file as that will not be interpreted by the server, making the server tags embedded in it useless. I don't want to define variables on the page as it seems awkward and error prone.

Instead, I created a new aspx file, put the javascript functions in there, and included that aspx file instead of the normal js file in my main template. This seems a bit unorthodox, but it seems to be okay.

Is there a flaw in my approach that I have not taken into account? Or some better (less obscure) method?

Edit . Bonus question. Should I use DOCTYPE inside the script file? After all, the script file is already included with the script tag. I tried to mimic regular js files, so I didn't specify any DOCTYPE.

+2


source to share


4 answers


Using a view, with the power of MVC templates, is a great way to accomplish this. It's easy to maintain, well understood, and gets the job done quickly. The only real trick is to get it to serve the correct content type. Doing something like this:

    public ActionResult ConfigurationSettings()
    {
        Response.ContentType = "text/javascript";
        return View(Configuration);
    }

      

You actually get text like text / html. The trick is to make sure you get the last word, add this to your controller:



    protected override void Execute(System.Web.Routing.RequestContext requestContext)
    {
        base.Execute(requestContext);
        requestContext.HttpContext.Response.ContentType = "text/javascript";
    }

      

And you will get the right type of content; The ViewResult seems to make it go text / html.

+2


source


The file extension, be it js, aspx, ashx, bla, foo, which is not that important yet. If you have server side generated javascript that is not page specific, then creating an ASPX page for rendering the javascript should be fine.



We will often use HTTP handlers to generate dynamic javvascript on our systems. We also don't forget to set the response headers to text / javascript so that the client browser knows we're sending back javascript.

+3


source


We use the ScriptDataBlock control from JsonFx to emit variables on the page itself. It acts like a dictionary where the key is the name of the variable (eg "MyNamespace.myVar") and this value is a normal C # value (including integer objects). The control generates appropriate namespace generation and type conversion to native JavaScript types. So, in that sense, it ends up not "awkward" or "error prone":

myDataBlock["MyApp.myVar"] = myObject;

      

If you're making an external file, then most likely your best .ashx handler. It will be lighter than a whole aspx page and will give you some pretty raw control over the output. The things you will want to do is set the "Content-Type" response header to "text / javascript" (technically incorrect, but the most common MIME type) and for Internet Explorer, which generally does not honor the Content-Type header as well set the "Content-Disposition" header to "inline; MyScriptName.js" so it knows the content extension.

Also, if these are actually "constants" and not calculated runtime data, you will need to set the "Expires" header at some future date to force the browser to cache the result. This will further reduce your server requests.

Edit: there is no DocType if you are building JavaScript. DocType is for markup only.

+1


source


Are you using the MVC framework (your question is tagged as such) correctly? If so, you can create an action that returns a JavaScriptResult to be executed on the page on load:

public class JSController : Controller {
    public ActionResult Headers() {
        // create your variables here
        return JavaScript("alert('hi');");
    }
}

      

And then you can add it to your aspx / master page:

<script src="/JS/Headers" type="text/javascript"></script>

      

+1


source







All Articles