I want to use unicode string in Object as key, something as:
var t = {"姓名": "naitong"};
it's ok , t["姓名"] return "naitong"
but
Object.keys({"姓名": "naitong"})
I got " ", a blank string
Anyone knowes why?
Editting:
I install firebug and try it in the console, it works.
Acctually i use mozrepl, so that i can editing and run javascript in emacs.
So This have something to do with mozrepl
I have confirm that mozrepl support only "7bit safe ASCII", to tranform unicode ,i have to json-encode it in emacs, as:
alert(Object.keys(JSON.parse("{\"\\u59d3\\u540d\":\"naitong\"}")))
This is my first question asked on stackoverflow, and i got quick resp.
Thank you all.
Works fine for me in the firebug console:
>>> Object.keys({"姓名": "naitong"})
["姓名"]
Maybe you are trying to display it on a page that uses a different charset which does not contain those symbols.
Related
I work on some crawler software of my own and one of my users just reported it will not work with code like this:
onclick="document.location.href = 'http://www.example.com/somepage.aspx'; return false;"
i.e. inside Javascript code use ' instead of ' to designate start end end of string
What surprises me is that browsers I have tested do not report any JavaScript syntax errors... And it seems to work when I click at it... I must be having a brain meltdown - is ' around a string really legitimate Javascript code?
is ' around a string really legitimate Javascript code?
No. The browser will decode the character references before evaluating the value as JavaScript.
I am using the QuickBlox JavaScript API. Looking through their code, I found this line:
var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
It appears that it has declared a string variable that is a regular expression pattern. Then it goes ahead to use that variable thus:
return str.replace(URL_REGEXP, function(match) {
url = (/^[a-z]+:/i).test(match) ? match : 'http://' + match;
url_text = match;
return '' + escapeHTML(url_text) + '';
});
I am wondering how is this possible? The var declared in the first line should be a string, but it is unquoted. Shouldn't this be a syntax error?
I went ahead and tested this code on my browser, and it works! This mean's I've got some learning to do here... Can anyone explain how this variable is declared?
Additionally, I tried to run the same code on my friends computer, the Chrome debugger throws a syntax error on the variable declaration line (unexpected token '/'). I am using Chrome Version 36.0.1985.143 m, my friend is using the same thing, but on my computer, it all works fine, on my friends computer, the code stops at the first variable declaration because of "syntax error".
Is there some setting that is different?
Any help would be appreciated.
UPDATE
Thanks for the quick answers. I've come from a PHP background, so thought that all regular expressions has to be initialized as strings :P.
Anyone can reproduce the syntax error I'm getting on my friends computer? (It still happens after disabling all extensions). I can't reproduce it either, and that's what is frustrating me.
UPDATE 2
I have tested and my friends computer and looked through the source. It appear to be due to some encoding problems (I'm not sure what). The regular expression is shown like this:
var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?芦禄鈥溾€濃€樷€橾))/gi;
(The characters at the end of the code is some random chinese characters, it seems).
How can I change the encoding to match his browser/system? (He is running on a Windows 7 Chinese simplified system).
It is not a String variable. It is a regular expression.
Calling var varname = /pattern/flags;
is effective to calling var varname = new RegExp("pattern", "flags");.
You can execute the following in any browser that supports a JavaScript console:
>>> var regex = /(?:[\w-]+\.)+[\w-]+/i
>>> regex.exec("google.com")
... ["google.com"]
>>> regex.exec("www.google.com")
... ["www.google.com"]
>>> regex.exec("ftp://ftp.google.com")
... ["ftp.google.com"]
>>> regex.exec("http://www.google.com")
Anyone can reproduce the syntax error I'm getting on my friends computer? (It still happens after disabling all extensions). I can't reproduce it either, and that's what is frustrating me.
According to RegExp - JavaScript documentation:
Regex literals was present in ECMAScript 1st Edition, implemented in JavaScript 1.1. Use an updated browser.
No, it shouldn't be a syntax error. In Javascript, RegExp objects are not strings, they are a distinct class of objects. /.../modifiers is the syntax for a RegExp literal.
I can't explain the syntax error you got on your friend's computer, it looks fine to me. I pasted it into the Javascript console and it was fine.
I found out that when I save this distorted string ("Äußerungen üben") as an ANSI text file, then open it with Firefox and choose in the Firefox menu "Unicode", it turns it into a readable german format ("Äußerungen üben").
The same thing is possible with my text editor (Notepad++).
Is there any way to achieve this with JavaScript? E.g. the following would be nice:
var output = makeReadable("Äußerungen üben");
Unfortunately, I get this kind of distorted strings from an external source which doesn't care about UTF-8 and provides all data as ANSI.
PS: Saving the file as UTF-8 and setting the charset as UTF-8 in the META Tag has no effect.
Edit:
Now I solved it through making a list of all common UTF8/ANSI distortions (more than 1300) and wrote a function replacing all wrong character combinations with the right character. It works fine :-) .
I think the encoding of the "distorted string" in your question got munged further by posting it here. But a quick Google search for "javascript convert from utf-8" returns this blog post as the top hit:
http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
So it turns out that encoding and decoding UTF-8 in JavaScript is really easy. This works great for me:
var original = "Äußerungen üben";
var utf8 = unescape(encodeURIComponent(original));
//return utf8; // something like "ÃuÃerungen üben"
var output = decodeURIComponent(escape(utf8));
return output;
Am facing issue in accessing the JSON object :
JSON Object am receiving is :
{"71":"Heart XXX","76":"No Heart YYYY"}
I tried to get the value of 71 and 72 separately and use it ...
but am getting some compile time issue as :
Syntax error on token ".71", delete this token
Code:
var map=$("#jsonText").val();
alert(map);
var obj=jQuery.parseJSON(map);
alert("JSON ::"+obj.71);
If am printing obj , am able to view [Object Object]
Can any one out there please help me to find the mistake i did ..I know the question above is asked in many threads in SO . Below are the few threads i found , but failed when i attempted to implement it ..
jquery json parsing
Also tried using the Jquery tutorial given in
Jquery JSON
Its working fine if the key is a String but getting the above error if its a number ...
Try this:
alert("JSON ::" + obj[71]);
"71" isn't a valid property identifier: an identifier should start with a letter, the underscore or the dollar sign. You can avoid this problem using square brackets instead.
Note: everything that's put between square brackets is converted into strings. Even functions, DOM elements or regular expressions: they're all converted with their toString methods, or their superclass' toString.
So 71 there is converted into "71". If you want a little more performance you can directly use the latter. If you don't need it, you can cut some key presses with just 71.
Use instead
alert("JSON ::"+obj["71"]);
according to the rules or javascript a identifier should not start by a number so if it starts by a number or for that matter contains spaces and other special charcters then you should access it by using the [] operator and not by . operator
so obj.71 is invalid but obj["71"] is
try using this site:
http://json.parser.online.fr/
I am trying to update the document.title with Javascript. This works fine, but now I want to insert the ü character into the title, which gives me a question mark at the place where the ü is supposed to be.
Here is my code (I omitted the rest of the title for clarity):
document.title = '\u2019 - \u252';
The strange thing is that the \u2019 part does work and gives me a quote, as expected. My charset is UTF-8, by the way.
What am I doing wrong here?
Do you want document.title = '\u00FC'; ? the &# and \u numbers are different in many places, or so i've found in my usages.
What's \u252? It doesn't seem to be a valid unicode escape, try \u0252?
Update: Apologies. I read it wrong. Thought you were looking for "ɒ" which is the result I get...
This worked for me on Chrome, Firefox and Safari (OSX)
document.title = '\u0252';