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?