开发者

Using "ñ" in a JS file

I have this form dropdown thats populated by a JS file. One of the options in my dropdown has the character ñ, but instead of the showing the letter it shows this: �.

How can I get the form to show the letter ñ?开发者_C百科 (something like an ascii code but for JS?)

thanks in advance...


First of all, fixing the encoding of the page (to UTF-8, preferably with <meta http-equiv="content-type" content="text/html; charset=utf-8"/> directly after <head>) and making sure your editor is set to UTF-8 is generally a very good idea.

Otherwise, replacing ñ with \u00f1 (in JavaScript code, not HTML) is the way to go.

If the JavaScript gets the ñ from an HTML or XML document, you can also use &#x00f1; or &ntilde; there.


You probably have wrong charset on your page. Check if you have something similar to this in your <head> tag:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

You need to have it UTF-8 encoded to see characters contained in it.

@phihag mentioned correctly that this answer will be only correct if the script is inline. For external scripts, you must ensure, that file is also UTF-8 encoded or include entity such as &ntilde;.


You should use UTF-8 in your page, as mentioned several times by other answers and comments.

For the actual character, you can either use the HTML entity &ntilde (again, as answered by others) or &#241; or &#xF1; (in HTML -- or in a JS string going to be inserted in or parsed as HTML), or the escaped UTF-8 value \u00F1 (in a JS string).


You could use the character code:

var babyGirl = "ni\u00f1a";


This is actually an HTML Entities issue. You need to provide the HTML entity instead of the character.

See: http://www.w3schools.com/tags/ref_entities.asp

Specifically: &nt ilde; (minus the space) -> ñ


Use &ntilde; See here


Try the html entity for the character

&ntilde;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜