Jonas
Jonas

Reputation: 784

Call session variables in external JavaScript file

I'm developping a number format changer for my JSP page. I have an some amounts which are manipulated by the script. I have 2 session variables, "userThousandSeparator" and "userDecimalSeparator" which contains 2 symbols, mostly . and ,.

The JSP file has following structure

    <html>
    <body>
    ...
    <span class="list-group-item list-group-item-sm">Instructed amount<span class="pull-right"><em id="instructedAmount"class="numberField">${instructedAmount}</em> <em id="instructedAmountCurrency">${instructedAmountCurrency}</em></span></span>

    <span class="list-group-item list-group-item-sm">Counter value amount<span class="pull-right"><em id="counterValueAmount class="numberField">${counterValueAmount}</em> <em id="counterValueAmountCurrency">${counterValueAmountCurrency}</em></span></span>
    ...
    <script src="js/numberFormatting.js"></script>
    </body>
    </html>

The numberFormatting.js contains following code to swap the thousand and decimal separators by the session variables:

function handleNumericFields(){
            $('.numberField').each(function(){
            var amount = $(this).text();
            var splittedValues = amount.split(/[,.]/);
            amount = "";

            for(var i=0 ; i < (splittedValues.length - 1) ; i++){
                amount += splittedValues[i];

                if(i < (splittedValues.length - 2)) {
                    amount += "${sessionScope.userThousandSeparator}";
                }
            }
            amount += "${sessionScope.userDecimalSeparator}";
            amount += splittedValues[splittedValues.length - 1]
            $(this).text(amount);
        });
}

$( document ).ready(function()   {
    handleNumericFields();
});

Now the problem is that when I add this in my jsp between <script>tags, this works perfectly. But now I want to add this script to a separated file, so I can use this for multiple jsp's, but now I get issues.

For example the number 500,005.00 gets formatted as 500${sessionScope.userThousandSeparator}005${sessionScope.userDecimalSeparator}00

Why doesn't my script know the session variables anymore and how to fix this?

Upvotes: 0

Views: 469

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48067

Your JS script is transferred to the browser as is. As it's not a JSP, you can't use EL in there. Obviously it's not processed and also just added as is in the script.

You can either

  • define more or less global variables and pick them up in your JS code
  • embed your JS code in the HTML page - or at least the configurable part, like calls to the function that you need
  • change your handleNumericFields function to accept parameters - e.g. thousandsSeparator and decimalSeparator. Or even just locale
  • implement your JS file as a JSP - but then you'd have to be careful with caching proxies (e.g. not recommended)

If possible, I'd recommend to introduce the parameters to the function. Or, if they're ever only those configuration-like values like "separator", you might get along with global variables. Not pretty, but does the job.

Upvotes: 1

Related Questions