Reputation: 8084
I have a need for generating JavaScript on the server. I can do this by using ASPX pages to template JavaScript but I'm getting warnings and I don't benefit from syntax hilighting which I would like. Is there a way to tell the .Net environment/Visual Studio that a certain ASPX page is supposed to output non-HTML text?
The reason for this is localization. My localized strings come from the server and I need these in JavaScript constructs on the pages. The easiest way I came up with is to have one JS file (I'll call it l18n.js for now) which defines the localized strings in a JSON object.
l18n = {
OK: <%= GetString("OK") %>,
Cancel: <%= GetString("Cancel") %>,
ConfirmDialog: {
Title: <%= GetString("ConfirmTitle") %>,
Message: <%= GetString("ConfirmMessage") %>
}
// .. etc.
}
This lets me use the localized strings in other parts of the UI
var okButton = $('<button>' + l18n.OK + '</button>')
But as said, just throwing the JavaScript inside an ASPX page, l18n.js.aspx, results in warnings and doesn't provide syntax hilighting. Is there a better way which would at least get rid of the warning? The current one is
Generation of designer file failed: Exception from HRESULT: 0x80042929
And in case it matters the project is built on top of .Net 2.0
Clarification
The language isn't user chosen so it shouldn't change all the time and I should be able to use the language value as the ETag so caching shouldn't be that big of an issue. From the browser perspective I'd consider the templated JS a static file just like rest of the files. I just don't want to declare the ~10 static JS files separately if I can automate it.
Upvotes: 2
Views: 917
Reputation: 17804
You could also load all your current pages language-elements in 'global' variables (constants) at the top of each page in a script block.
var LANG_OK = 'ok string';
var LANG_CANCEL = 'cancel string';
var LANG_CONFIRM_TITLE = 'title string';
var LANG_CONFIRM_MESSAGE = 'message string';
This keeps the dynamic part away from your javascript file. This means the javascript files are static like they are supposed to be.
update
You could consider creating javascript repositories for each language based on my solution above.
This would mean you just have to select which file to include in the page:
<% Response.Write "<script type='text/javascript' src='/js/language_" & User.LanguageCode & ".js'></script>" %>
Upvotes: 1
Reputation: 189457
An ASPX is a ASP.NET Form. It is expected to represent a Web Form which the user may interact with running further code in the ASPX. Hence the ASPX lifecycle represents much of this.
For raw output such as JS you should use an ASHX which does not have all this Forms overhead.
In your specific case I would be looking into creating a set of classes that can be serialised as JSON. Send the serialised JSON as the output of your ASHX.
Upvotes: 2
Reputation: 11266
You could make a control (.ascx) that consists of a script block?
To get jquery intellisense, do something like this (it's for asp.net mvc but something similar should work for classing asp.net too):
<% if (false)
{ %>
<script src="../../Static/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<% } %>
Upvotes: -1