nfvindaloo nfvindaloo - 1 year ago 72
Javascript Question

Extra  Character added by .js script

Using jQuery and the formatCurrency extension as below:

$(function() {
$(".currency").blur(function(){ formatCurrencyInput(this) });

function formatCurrencyInput(input) {
if($(input).val() != '') {
if(isNumeric($(input).val())) {
$(input).css('border-color', '');
else {
$(input).css('border-color', '#FFCCCC');

All my text inputs with the "currency" class are converted from 45 => £45.00 for example.

The weird thing is if the formatCurrencyInput function is defined in the section it works fine. If I externalise the function into a .js file, it returns £45.00 (note the  character). I guess this is a character encoding issue, but how do I fix this?


Answer Source

If you use any non-Ascii character in a JavaScript file, you should make sure that the server sends proper HTTP headers for it, identifying the character encoding. Other methods, such as a charset parameter in the script element used to refer to the file, or relying on the browser defaulting the encoding somehow (e.g., from the referring HTML document), as less reliable.

But if you have just a single non-Ascii character in a string literal, the simplest way is to escape it. The pound sign “£” U+00A3 can be escaped, inside a string literal, as \xA3 or as \u00A3.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download