8

How can I set the resource string in javascript?

I have a resource file called, for example,

myResourceFile.resx

in my code and using the literal control I can use:

lblName.Text = Resources.myResourceFile.ajaxRetrievingInformation;

<asp:Literal id="lit" runat="server" 
             Text="<%$ Resources:myResourceFile, ajaxRetrievingInformation%>" />

but if I try this in javascript, like:

<asp:Button ID="btnImportCompaniesAndEmployees" 
            runat="server" 
            CssClass="myButtonCssClass"
            OnClick="btnImportCompaniesAndEmployees_Click"
            OnClientClick="strLoadingText='<%$ Resources:myResourceFile, ajaxRetrievingInformation%>';return true;"
            ...
/>

or

<script type="text/javascript">
    var strLoadingText = '<%$ Resources:myResourceFile, ajaxRetrievingInformation%>';
</script>

I get errors...

Does anyone know how to manage this? Something like:

var strLoadingText = Resources.GetString(myResourceFile, ajaxRetrievingInformation);

Thanks

1 Answer 1

16

You should be able to use:

<script type="text/javascript">
    var strLoadingText = "<%= Resources.myResourceFile.ajaxRetrievingInformation %>";
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

how easy can that be! :) Thanks
MVC 3: var errorBlank = '<asp:Literal runat="server" Text="<%$ Resources:LocalizedText, General_Error%>" />';
what happens if the string has as pecial character say ' apostophe
@AlanFord, an apostrophe won't hurt anything, but switch to <%: if you want it safely encoded. I use <%: exclusively these days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.