I want to show an alert message in two different language. I am using asp.net App_GlobalResources. Can I use it inside my script.js file?
You can't call directly to resources in a RESX file from a javascript files.
However, you could create a partial view (in case you are using MVC) from which you can access all those resources. You could, thereafter, include that partial inside your pages.
For example, you could do a partial in which you would have:
<script>
var Resources = {
Name: '@Resources.tags.Name',
Surname: '@Resources.tags.Surname',
};
</script>
After this, you could include this page in the pages you need and access from javascript to these resources by using:
Resources.Name
If you are not using MVC, please let me know to look for the way it should be done in ASP.NET.
If you have any doubt, please say.
For WebForms
In case you use WebForms, you coud make use of a user control, in which you will configure the javascript to be injected in the page.
Thereafter, include that user control in your page (preferably the master to make it available through all your website) and you will be able to access it.
The user control would look this way:
public partial class resources : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
LiteralControl jsResource = new LiteralControl();
jsResource.Text = "<script type=\"text/javascript\">";
jsResource.Text += "var Resources = {";
jsResource.Text += "Name: '" + Resources.Resource.Name + "',";
jsResource.Text += "Surname: '" + Resources.Resource.Surname + "',";
jsResource.Text += "};";
jsResource.Text += "</script>";
Page.Header.Controls.Add(jsResource);
}
}
Then you would include that control in your page:
<uc1:resources runat="server" ID="resources" />
And you could simply access your javascript by doing so:
<script>
alert(Resources.Name);
alert(Resources.Surname);
</script>