Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).formatCurrency(null,{groupDigits:false,roundToDecimalPlace:2,symbol:'£'});
                    $(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?

like image 250
nfvindaloo Avatar asked Nov 12 '12 21:11

nfvindaloo


2 Answers

Try:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
...

You could also try changing £ to &pound;.

like image 111
Coby Avatar answered Oct 02 '22 19:10

Coby


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.

like image 30
Jukka K. Korpela Avatar answered Oct 02 '22 20:10

Jukka K. Korpela