admin管理员组

文章数量:1327843

i am running into trouble with JavaScripts strToLower()

var s = 'tür';
alert(s);
alert(s.strToLower());

should result in the same output. however the output is different:

1: tür
2: tã¼r

any suggestions how to handle the uft8-special correctly if using strToLower()?

Martin

i am running into trouble with JavaScripts strToLower()

var s = 'tür';
alert(s);
alert(s.strToLower());

should result in the same output. however the output is different:

1: tür
2: tã¼r

any suggestions how to handle the uft8-special correctly if using strToLower()?

Martin

Share Improve this question asked Apr 3, 2012 at 9:53 Martin AbrahamMartin Abraham 7627 silver badges25 bronze badges 3
  • 3 JavaScript has no strToLower function. PHP does, and a quick search reveals at least one person has ported it to JavaScript for some reason, but JavaScript itself doesn't have one. The JavaScript function, toLowerCase, doesn't seem to exhibit the behavior you mention on Chrome, Firefox, or Opera on Linux or on IE9 or Safari on Windows 7. – T.J. Crowder Commented Apr 3, 2012 at 9:55
  • Your page is encoded as utf8, but your server wrongly tells it's ISO-8859-1. Fix your headers. – georg Commented Apr 3, 2012 at 9:59
  • Flagged as Too Localized -- given that JavaScript doesn't have this function and you haven't responded to ments, it's impossible for anyone to say what's going on here. – Matthew Read Commented May 7, 2012 at 21:15
Add a ment  | 

5 Answers 5

Reset to default 4

JavaScript's native .toLowerCase() method handles UTF-8 just fine:

alert( "tür".toLowerCase() );

Your page encoding might need to be set to UTF-8 with a header or meta-tag.

There is a toLowerCase function and it should work ok as Javascript uses UTF8 internally. Check:

http://jsfiddle/5k5hv/

You have two built-in functions to convert to lowercase: .toLowerCase() and .toLocaleLowerCase(). Both should work just fine with the example you provide, but you need to make sure you've saved your source code as UTF-8. The exact procedure heavily depends on your IDE or editor.

Additionally, it won't hurt if your web server appends the charset to Content-Type headers.

Make sure the file you are working with is encoded into "UTF-8 without BOM" (option in Notepad++) and add <META http-equiv="Content-Type" content="text/html; charset=EUC-JP"> inside <head>

I tried and works.

Your code makes no sense. If you assign tür to a string, then you will always get tür back. You are probably omitting a step from your code, such as sending the string over AJAX to a server. To do this, the string will have to be encoded, usually using UTF-8, in which case the correct encoding is tür as you observed. However if the server is working in ISO-8859-1 instead of UTF-8 then it will think that you original string was tür and will attempt to lowercase it to tã¼r as described. This will cause problems when the front end tries to interpret it as UTF-8. The solution is to ensure that the server is also working in UTF-8.

本文标签: utf 8Javascript strToLower()uft8 and (german) special charactersStack Overflow