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 ñ
or ñ
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 ñ
.
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 ñ
(again, as answered by others) or ñ
or ñ
(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 ñ
See here
Try the html entity for the character
ñ
精彩评论