There are two ways I’ve found on how to call a resource file from JaveScript and it is largely dependent on where your JavaScript lives. Does it live within a <SCRIPT> tag embedded within your aspx page or does live within a .js file.
- Embedded within a <SCRIPT> tag within your aspx page:
[js light=”true”]
<script type="text/javascript">
alert(‘"<%=Resources.ResxFileName.FieldName %>"’);
</script>
[/js] - Embedded within a .js fileThis method evolves around the using a hidden field to get the value and then passing that into the javascript file.
This code lives within the .aspx file
[csharp light=”true”]
<asp:HiddenField ID="hdnSearchDefaultText" runat="server"
Value="<%$Resources:ResxFileName, FieldName %>" /><asp:TextBox ID="SearchTextBox" runat="server"
Text="<%$Resources:ResxFileName, FieldName %>"
onfocus="SetDefaultKeywords(this, ‘hdnSearchDefaultText’);"
onblur="SetDefaultKeywords(this, ‘hdnSearchDefaultText’);"/>
[/csharp]This code lives within the .js file.
[javascript light=”true”]
function SetDefaultKeywords(obj, controlName) {
var defaultKeyword = $(‘input[id*="’+controlName+’"]’).val();
if (obj.value == defaultKeyword) {
obj.value = ”;
}
else if (obj.value == ”) {
obj.value = defaultKeyword;
}
}
[/javascript]