views:

80

answers:

2

Hello, we're facing a problem now. We have a pretty big page with a loooong inline script, which does all the work. We now want to obfuscate it and use it as a separate .js file. But the problem is that we have paths which are generated by Url helper (Url.Content()). So what is the best way to separate js file from the page and not using hard-coded path strings?

A: 

You could create javascript functions to set the required paths and invoke them from a small script section from the page.

Javascript file:

var resource1;
var resource2;

function setResourcesReferences(resource1, resource2, ...) {

}

ASPX file:

<script type="text/javascript">
   setResourcesReferences(<% Url.Content("Resource1") %>, <% Url.Content("Resource2") %>, ...);
</script>
Anero
+3  A: 

I usually write my javascript in separate views (with only js code) and use my own action result to render it. This way I can take advantage of c# on the server side and I can use a model if needed and it will be included as a external js file in the browser (with appropriate caching). The action result I use can be found here: http://codepaste.net/p2s3po

Update

You can use the action result like this from your action:

public ActionResult JsFile() {
    ViewData.Model = //Create model if you want one;
        return new JavascriptFileResult(true)
                   {
                       TempData = TempData,
                       ViewData = ViewData
                   };
}

Then you just treat it as if it was a normal view (but only write javascript in the view). You can take any number of parameters as well of course.

You can include it like this:

<script type="text/javascript" src="<%=Url.Action("JsFile", "ControllerName")%>"></script>
Mattias Jakobsson
That sounds like a good solution. But where do RegexReplace method and JavaScriptCompressor class come from in the code snippet you've sent? And one more question — how exactly should I use JavascriptFileResult from my action?
HiveHicks
@HiveHicks, Sorry, forgot to mention that. They are both from the Yahoo.Yui.Compressor library that you can download here: http://yuicompressor.codeplex.com/. I'll update my answer to show you how to use the action result.
Mattias Jakobsson
Great, thanks! Seems like the best solution.
HiveHicks
@HiveHicks, For me it has worked out very well. The same could be done with css if you need dynamic css with values from the db.
Mattias Jakobsson
+1 Mattias, I know it's been a while since you 'upped' this. just wanted to say, it works really well. any comments/additions since this 1st draft?? i'm hoping to fully test this and incorporate it into a production app, is there anything i should be aware of??
jim
Mattias, i also took the liberty of overloading the JavascriptFileResult initializer with a parameter to supply an alternative filename for the 'view'. this means you can call the jsFile action with a string jsFilename parameter which invokes a view of the same name, rather than looking for jsfile.as(pc)x. just thought that might interest you. i'll post my changes tho' you will of course be able to figure what i did i guess :)
jim