When I develop application, I want to use jquery-1.3.2.js, and when I deploy it, I want to use jquery-1.3.2.min.js?
What is the best way to switch between two without manually commenting and uncommenting one.
Thanks.
min. js is a compressed version of jquery. js (whitespaces and comments stripped out, shorter variable names, ...) in order to preserve bandwidth. In terms of functionality they are absolutely the same.
The sole benefit of minified jQuery file is allowing a client to download fewer bytes , enabling the page to load faster, use less battery, use less of a mobile data plan, etc. The jQuery-x.x.x-min. js file will be more than 50% less than the normal js file. Reduction in the file size makes the web page faster .
I have a HtmlHelper extension method to load my JavaScript files. It looks something like this...
public static string JavascriptTag(this HtmlHelper html, string javascriptName)
{
var format = "<script src=\"/Content/Scripts/{0}.js\" type=\"text/javascript\"></script>\r\n";
#if (!DEBUG)
javascriptName += ".min";
#endif
return string.Format(format, javascriptName);
}
Which is called very simply like:
<%= Html.JavascriptTag("jquery-1.3.2") %>
<%= Html.JavascriptTag("general") %>
I can use this convention because I also have a build task that takes all JS files inside my content/scripts directory, minifies them and then inserts .min into the filename.
If you don't have that luxury, you could use a variation like so:
public static string JavascriptTag(this HtmlHelper html, string devFileName, string liveFileName)
{
var format = "<script src=\"{0}\" type=\"text/javascript\"></script>\r\n";
var fileNameToUse = devFileName;
#if (!DEBUG)
fileNameToUse = liveFileName;
#endif
return string.Format(format, fileNameToUse );
}
Which could then be called like:
<%= Html.JavascriptTag("/Content/Scripts/jquery-1.3.2.js", "/Content/Scripts/jquery-1.3.2.min.js") %>
<%= Html.JavascriptTag("/Content/Scripts/general.js", "/Content/Scripts/general.min.js") %>
An added bonus of the approach above is that you can use a CDN for libraries, e.g.
<%= Html.JavascriptTag("/Content/Scripts/jquery-1.8.2.js", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js") %>
Name both files jquery.js for example, and put in some folder (libs) When you deply, don't deploy lib folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With