Using the resource file from within JavaScript

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.

  1. Embedded within a <SCRIPT> tag within your aspx page:
    [js light=”true”]
    &lt;script type=&quot;text/javascript&quot;&gt;
            alert(‘&quot;&lt;%=Resources.ResxFileName.FieldName %&gt;&quot;’);
    &lt;/script&gt;
    [/js]
  2. 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”]
    &lt;asp:HiddenField ID=&quot;hdnSearchDefaultText&quot; runat=&quot;server&quot;
    Value=&quot;&lt;%$Resources:ResxFileName, FieldName %&gt;&quot; /&gt;

    &lt;asp:TextBox ID=&quot;SearchTextBox&quot; runat=&quot;server&quot;
    Text=&quot;&lt;%$Resources:ResxFileName, FieldName %&gt;&quot;
    onfocus=&quot;SetDefaultKeywords(this, ‘hdnSearchDefaultText’);&quot;
    onblur=&quot;SetDefaultKeywords(this, ‘hdnSearchDefaultText’);&quot;/&gt;
    [/csharp]

    This code lives within the .js file.

    [javascript light=”true”]
    function SetDefaultKeywords(obj, controlName) {
        var defaultKeyword = $(‘input[id*=&quot;’+controlName+’&quot;]’).val();
        if (obj.value == defaultKeyword) {
            obj.value = ”;
        }
        else if (obj.value == ”) {
            obj.value = defaultKeyword;
        }
    }
    [/javascript]