我想在我的JavaScript代碼中使用全局資源。通常情況下,當代碼在ASP代碼中時,我使用 <%=GetGlobalResourceObject("Resource", "MONTHS_SHORT1") %>
並且它可以工作。但是,當JavaScript代碼不在ASP代碼中時,例如在calendar.js文件夾中,它不起作用。在這種情況下如何使用全局資源?注意:資源文件位於我的App_GlobalResources文件夾內。在JavaScript中使用全局資源
2
A
回答
2
看看下面的文章,我創建的描述瞭如何連載全球和本地資源和使用jQuery和JSON的JavaScript檢索。
http://bloggingdotnet.blogspot.com/2010_02_01_archive.html
首先,創建一個新的處理程序(ashx的)文件。這是一個相當長的一段時間前寫的,所以使用vb.net和自定義JSON序列:
Imports System.Web
Imports System.Web.Services
Imports System.Xml
Imports System.Resources
Imports System.Reflection
Public Class Localisation
Implements System.Web.IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim files As String = context.Request.QueryString("files")
Dim local As String = context.Request.QueryString("local")
Dim isLocal As Boolean
Dim folder As String = "App_GlobalResources"
context.Response.ContentType = "text/javascript"
'Write out file as object
context.Response.Write("{")
'Determine if local resource file
If local IsNot Nothing Then
isLocal = CBool(local)
If isLocal Then folder = "App_LocalResources"
End If
If files Is Nothing OrElse files.Length = 0 Then Throw New ArgumentException("Parameter 'files' was not provided in querystring.")
Dim flag As Boolean = False
For Each file As String In files.Split(",")
If flag Then context.Response.Write(",")
Dim className As String = file.Split(".")(0)
'Write the class (name of the without any extensions) as the object
context.Response.Write(className)
context.Response.Write(":{")
'Open the resx xml file
Dim filePath As String = context.Server.MapPath("~\" & folder & "\" & file)
Dim document As New XmlDocument()
Dim flag2 As Boolean = False
document.Load(filePath)
Dim nodes As XmlNodeList = document.SelectNodes("//data")
For Each node As XmlNode In nodes
'Write out the comma seperator
If flag2 Then context.Response.Write(",")
Dim attr As XmlAttribute = node.Attributes("name")
Dim resourceKey As String = attr.Value
context.Response.Write(resourceKey)
context.Response.Write(":""")
'Write either the local or global value
If isLocal Then
context.Response.Write(HttpContext.GetLocalResourceObject(String.Format("~/{0}", file.Replace(".resx", "")), resourceKey)) 'Has to be full path to the .aspx page
Else
context.Response.Write(HttpContext.GetGlobalResourceObject(className, resourceKey))
End If
context.Response.Write("""")
'Flag that we need a comma seperator
flag2 = True
Next
context.Response.Write("}")
flag = True
Next
'End file
context.Response.Write("}")
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
當正在工作,請使用以下的jQuery代碼發出AJAX調用HTTP處理程序和返回的內容資源文件作爲對象文字。
// -- Localisation --
var localResources;
var globalResources;
//Sample JSON for javascript resource values eg {TrackDetail:{HideHelp:"Hide Help", ShowHelp:"Show Help"}}
//Usage e.g: alert(localResources.TrackDetail.HideHelp);
//Load Localisation values into variables so that they can be available on the client
//Note that these values are loaded asynchronously, the code in the function will not run until the call has completed.
$.getJSON('Localisation.ashx?files=TrackDetail.aspx.resx&local=true', function(data) { localResources = data});
$.getJSON('Localisation.ashx?files=Errors.resx,Strings.resx', function(data) { globalResources = data});
1
您必須生成模擬您所需資源的JavaScript代碼,因爲ASP.NET運行在服務器端,而JavaScript運行在客戶端。
<script language="javascript" type="text/javascript">
var MONTHS_SHORT1 = <%=GetGlobalResourceObject("Resource", "MONTHS_SHORT1") %>;
</script>
1
我經常使用一個隱藏的文本保存資源文本,然後搶使用JavaScript和jQuery的本地化的文本:
<asp:Literal runat="server" ID="Literal1" visible="false" Text="<%$ Resources:String, MyResourceText%>" />
<input type="button" id="Button1" value="" />
<script type="text/javascript">
$(document).ready(function() {
// Update the buton text with the correct localised lookup
var ButtonText = '<%=Literal1.Text%>';
$("#Button1").attr('value', ButtonText);
});
</script>
0
這將註冊資源鍵。
ClientScriptManager cs = Page.ClientScript;
String scriptRegVariables = string.Format("var resourcetext = '{0}'", Resources.Resource.keyText);
if (!cs.IsClientScriptBlockRegistered("RegVariablesScript"))
{
cs.RegisterClientScriptBlock(typeof(_Default), "RegVariablesScript", scriptRegVariables, true);
}
現在在.js文件裏面可以直接使用它。 例如警報(resourcetext);
將上面的代碼添加到Page_Load的控件或aspx頁面
相關問題
- 1. pthreads在PHP中訪問全局資源
- 2. WPF在App.xaml中的全局資源
- 3. 文本全局資源propery
- 4. 如何在WPF中使用全局資源?
- 5. 在MVC中使用沒有app_globalresources的全局資源文件
- 6. 在XAML中使用全局資源(Application vs Page)
- 7. 全局磁盤資源不可用
- 8. 類庫中的全局資源
- 9. Struts2中的全局資源包
- 10. 有條件地使用標籤中的全局資源的值
- 11. 在javascript web資源中使用'GetGlobalContext'
- 12. 在javascript中使用全局變量
- 13. 在JavaScript中使用全局變量
- 14. javascript在全局對象中使用'this'
- 15. 在使用全球資源時未發現強類型資源
- 16. 設置角度$資源全局配置
- 17. ASP.NET MVC全局資源文件部署
- 18. Windows Phone 7:未找到全局資源
- 19. 如何閱讀全局資源文件
- 20. SpringBoot Tomcat嵌入式全局JNDI資源
- 21. 作爲全局jndi資源的hibernate sessionfactory
- 22. 使用全局資源連接到redis服務器的原因
- 23. 如何在視圖中使用asp.net mvc razor訪問全局資源
- 24. JavaScript使用全局變量
- 25. Wicket應用程序中全局可訪問的資源
- 26. ASP.NET - 在全局資源文件中存儲SQL查詢?
- 27. 如何在ActiveAdmin 0.5.0中爲資源指定全局加載?
- 28. 如何在REST API中爲全局資源建模?
- 29. 使用JavaScript卸載HTML中的資源
- 30. 如何使用佈局,選擇資源