473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

eval and non-English characters conflict?

Yes, eval is a tool of the devil and I'll burn for using it. However,
in this instance it's quite handy and I'm quite lazy.

So, here's a weird one, and I'm wondering if anyone has a workaround.
I am pulling data off a server via AJAX, and some of that data has
non-English characters in it. The data is in the form of a json:

{"exitCode":1,"className":"clientRecords"
,"strAccountId":"100"
,"strName":"Dr. Gary A. Martin"
,"intReportsPurchased":100
,"intReportsUsed":13
,"intBalance":87
,"clientData":{
"r19":{
"className":"client"
,"intId":353
,"strFname":"NameOf"
,"strLname":"Someone"
,"strMname":""
}
,"r7":{
"className":"client"
,"intId":251
,"strFname":"BananaHead"
,"strLname":"Myrbø"
,"strMname":""
}
}

For testing, I was using eval("json =" +response.text). This works
fine for any records that DON'T have a non-English character in them.
However, when it has something like 'Myrbø' with the funny o on the
end, it dies and throws an error dialog saying "Expected '}'". Why
would it choke on the funny character? If I copy and paste the json
directly into the code, it works fine; it's just when it's embedded in
the eval that it goes tits up.

For extra strangeness, this does NOT happen on IE5.5, IE7, FF1.5.x or
FF1.0.x - just on IE 6 SP2!

Has anyone ever seen suchlike before???

Tyler

Nov 7 '06 #1
12 3750
Logos wrote:
So, here's a weird one, and I'm wondering if anyone has a workaround.
I am pulling data off a server via AJAX, and some of that data has
non-English characters in it.
For extra strangeness, this does NOT happen on IE5.5, IE7, FF1.5.x or
FF1.0.x - just on IE 6 SP2!
How is the data encoded when you send it from the server? Try sending
UTF-8 encoded data. Although it is somehow strange that it works with IE
5.5 and IE 7 but not with IE 6.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 7 '06 #2
How is the data encoded when you send it from the server? Try sending
UTF-8 encoded data. Although it is somehow strange that it works with IE
5.5 and IE 7 but not with IE 6.

It's straight ASCII from a file on the server. Unfortunately our
choice of back ends doesn't allow any other encoding. The truly weird
thing is is that I copy and paste it directly into my code, and it
works; copy and paste the exact same snippet into my code but inside an
eval, and it bombs. The odd 'o' character trips it up. Go ahead and
try it in IE, it's neat to watch.

Nov 8 '06 #3
"Logos" <ty*********@gmail.comwrote in news:1162912848.664818.170200
@m73g2000cwd.googlegroups.com:
For testing, I was using eval("json =" +response.text).
Try eval('"json =" +response.text') and see if that makes a difference.
Nov 8 '06 #4
Logos wrote:
Yes, eval is a tool of the devil and I'll burn for using it. However,
in this instance it's quite handy and I'm quite lazy.

So, here's a weird one, and I'm wondering if anyone has a workaround.
I am pulling data off a server via AJAX, and some of that data has
non-English characters in it. The data is in the form of a json:

{"exitCode":1,"className":"clientRecords"
,"strAccountId":"100"
,"strName":"Dr. Gary A. Martin"
,"intReportsPurchased":100
,"intReportsUsed":13
,"intBalance":87
,"clientData":{
"r19":{
"className":"client"
,"intId":353
,"strFname":"NameOf"
,"strLname":"Someone"
,"strMname":""
}
,"r7":{
"className":"client"
,"intId":251
,"strFname":"BananaHead"
,"strLname":"Myrbø"
,"strMname":""
}
}
For testing, I was using eval("json =" +response.text). This works
fine for any records that DON'T have a non-English character in them.
This example shouldn't work fine. The braces are not balanced. I seems that you
are not using a proper JSON encoder.

I recommend that you use eval as minimally as possible.

var json = eval('(' + response.text ')');
However, when it has something like 'Myrbø' with the funny o on the
end, it dies and throws an error dialog saying "Expected '}'". Why
would it choke on the funny character? If I copy and paste the json
directly into the code, it works fine; it's just when it's embedded in
the eval that it goes tits up.
That should happen if you are transmitting invalid crap.
It's straight ASCII from a file on the server. Unfortunately our
choice of back ends doesn't allow any other encoding. The truly weird
thing is is that I copy and paste it directly into my code, and it
works; copy and paste the exact same snippet into my code but inside an
eval, and it bombs. The odd 'o' character trips it up.
Straight ASCII has no funny characters. Who knows what you are transmitting.

JSON requires Unicode, ideally UTF-8 (see RFC 4627
(http://www.ietf.org/rfc/rfc4627.txt?number=4627). You didn't specify why your
back end doesn't allow correct character encoding. I suspect it is due to either
laziness, as you explained in your introduction, or incompetence.
Nov 8 '06 #5
VK
Logos wrote:
The odd 'o' character trips it up. Go ahead and
try it in IE, it's neat to watch.
The "odd 'o' character" shall be "latin small letter o with stroke"
(ISO 8859-1 code 248, Unicode 00F8, HTML entity oslash)

Now I'll play a clairvoyant (which is rather easy though in your case):

Your page is in ISO 8859-1 encoding but (to make it "cooler looking")
you have placed charset=UTF-8 in the head section or your server
wrongly reports it as UTF-8.
Alas many authors and admins do it because they've been taught that
"Unicode is cool, anything else is lame" - and who wants to look lame?
:-)

It is alas because UTF-8 is not the indication of the actual encoding
on the page: the page is in Unicode encoding. UTF-8 is *transport*
encoding: it tells to the recipient what algorithm was used to encode
multibyte Unicode sequences to deliver them over single-byte media
(which is HTTP packets). Having this information provided, parser can
decode properly the single byte stream back to Unicode characters.

Unicode characters 0-127 have the same code value as the corresponding
ASCII characters and they are not encoded in UTF-8. This way the parser
(even with wrong encoding indicated) has no problems to parse them. But
suddenly it meets your byte with value 248 (your oslash). As it was
instructed that the transport encoding is UTF-8, it presumes that this
byte is the beginning of a UTF-8 multibyte sequence denoting some
Unicode character. In attempt to read this sequence in full it
"swallows" one or two bytes immediately following the byte with 248
value. Whatever follows it: it will collapse. It can be curly bracket:
then you'll get "} is missing". It can be closing quote: then you'll
get "Unterminated string constant".

IE7 (and other modern UA's) doesn't chock on it because "bogus UTF-8"
became so common that UA producers had to add extra neuristic to the
parser to sort out real UTF-8 and "beautifying UTF-8". That was much
easier than to educate the web community what UTF-8 is, what Unicode is
and how do they relate to each other.

Here is your case:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script>
var str = "{'foo':'barш'}";

eval('var foo=' + str);

alert(foo.foo);
</script>
</head>

<body>

</body>
</html>

simply change UTF-8 to ISO-8859-1 and it will come back to life.

Nov 8 '06 #6
VK

VK wrote:
Here is your case:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script>
var str = "{'foo':'barш'}";

eval('var foo=' + str);

alert(foo.foo);
</script>
</head>

<body>

</body>
</html>

simply change UTF-8 to ISO-8859-1 and it will come back to life.
You can make it work as it is on IE6 if you have additional Auto-Select
module installed and activated (View Encoding Auto-Select). It
implements the required neuristic which is built-in into IE7 by
default. It has other drawbacks though (search this group for "Korean
issue in IE" for a sample) so I wouldn't suggest to have it activated
permanently: at least until all Web developers will learn to always
indicate encoding for their pages and to match the declared encoding to
the actual one. The achievement of this "halliloya" should be placed to
some very far feature, I'm affraid :-( :-)

P.S. To make what I said more "visual":
1) Imagine you see a string "wninfpevcg" and I tell you that this is
ROT13 encoding. You go to any ROT13 decoder and you get the string
"javascript". No problem.
2) Now imagine you would misinform you by telling that this is ROT47.
You go to a ROT47 decoder and you are getting an obscure sctring
"H?:?7A6G48"

The case 2) is your case, where encoded "javascript" string is your
web-page, bad guy telling a wrong encoding is yourselve (or your server
admin) and "H?:?7A6G48" instead of "javascript" is the result of not
telling the true.

Nov 8 '06 #7

Douglas Crockford wrote:
This example shouldn't work fine. The braces are not balanced. I seems that you
are not using a proper JSON encoder.
Twas a copy and paste error. The ending brace is there in the
transmission.
I recommend that you use eval as minimally as possible.
Truly, your wisdom is mighty. However, see disclaimer in original
post.
That should happen if you are transmitting invalid crap.
Really? Why only in IE6 and only when using an eval statement then, O
all knowing one? Though I know it's beneath you, let's address the
entire issue just for kicks, hmmm?
It's straight ASCII from a file on the server. Unfortunately our
choice of back ends doesn't allow any other encoding. The truly weird
thing is is that I copy and paste it directly into my code, and it
works; copy and paste the exact same snippet into my code but inside an
eval, and it bombs. The odd 'o' character trips it up.

Straight ASCII has no funny characters. Who knows what you are transmitting.
Haven't looked at the hex, but it's stored as ASCII; it certainly
doesn't display correctly when examined on the back end, it shows as an
interesting star like character. As for what I am transmitting, I
posted it, tho copy & paste may garble the actual character codes.
JSON requires Unicode, ideally UTF-8 (see RFC 4627
(http://www.ietf.org/rfc/rfc4627.txt?number=4627). You didn't specify why your
back end doesn't allow correct character encoding. I suspect it is due to either
laziness, as you explained in your introduction, or incompetence.
No, it's because you're an asshole. It's filePro on SCO UNIX. As far
as I know filePro doesn't allow anything but ASCII, but since you are
obviously enormous talented in every direction you must also be a guru
in legacy 80s databases hosted on poorly doucmented crappy OSs and will
be sure to enlighten me further.

Or you could just be a total asshole, that's a really strong
possibility too.

Nov 8 '06 #8
Hm...I didn't specify any encoding actually. Me being lazy again! I
will try inserting your suggested code and seeing if it makes magic :)

Thanks for the suggestion!

Tyler

VK wrote:
Logos wrote:
The odd 'o' character trips it up. Go ahead and
try it in IE, it's neat to watch.

The "odd 'o' character" shall be "latin small letter o with stroke"
(ISO 8859-1 code 248, Unicode 00F8, HTML entity oslash)

Now I'll play a clairvoyant (which is rather easy though in your case):

Your page is in ISO 8859-1 encoding but (to make it "cooler looking")
you have placed charset=UTF-8 in the head section or your server
wrongly reports it as UTF-8.
Alas many authors and admins do it because they've been taught that
"Unicode is cool, anything else is lame" - and who wants to look lame?
:-)

It is alas because UTF-8 is not the indication of the actual encoding
on the page: the page is in Unicode encoding. UTF-8 is *transport*
encoding: it tells to the recipient what algorithm was used to encode
multibyte Unicode sequences to deliver them over single-byte media
(which is HTTP packets). Having this information provided, parser can
decode properly the single byte stream back to Unicode characters.

Unicode characters 0-127 have the same code value as the corresponding
ASCII characters and they are not encoded in UTF-8. This way the parser
(even with wrong encoding indicated) has no problems to parse them. But
suddenly it meets your byte with value 248 (your oslash). As it was
instructed that the transport encoding is UTF-8, it presumes that this
byte is the beginning of a UTF-8 multibyte sequence denoting some
Unicode character. In attempt to read this sequence in full it
"swallows" one or two bytes immediately following the byte with 248
value. Whatever follows it: it will collapse. It can be curly bracket:
then you'll get "} is missing". It can be closing quote: then you'll
get "Unterminated string constant".

IE7 (and other modern UA's) doesn't chock on it because "bogus UTF-8"
became so common that UA producers had to add extra neuristic to the
parser to sort out real UTF-8 and "beautifying UTF-8". That was much
easier than to educate the web community what UTF-8 is, what Unicode is
and how do they relate to each other.

Here is your case:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script>
var str = "{'foo':'barш'}";

eval('var foo=' + str);

alert(foo.foo);
</script>
</head>

<body>

</body>
</html>

simply change UTF-8 to ISO-8859-1 and it will come back to life.
Nov 8 '06 #9

Jim Land (NO SPAM) wrote:
"Logos" <ty*********@gmail.comwrote in news:1162912848.664818.170200
@m73g2000cwd.googlegroups.com:
For testing, I was using eval("json =" +response.text).

Try eval('"json =" +response.text') and see if that makes a difference.
I don't think that will work - won't that put a string on the left hand
side of an assignment statement, rather than a variable name?

Nov 8 '06 #10
Got it in one, Mister! You are my hero, and are awarded the official
title of Javascript GOD.

May I worship at your toesie-wosies? Please? <grin>

Tyler

VK wrote:
VK wrote:
Here is your case:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script>
var str = "{'foo':'barш'}";

eval('var foo=' + str);

alert(foo.foo);
</script>
</head>

<body>

</body>
</html>

simply change UTF-8 to ISO-8859-1 and it will come back to life.

You can make it work as it is on IE6 if you have additional Auto-Select
module installed and activated (View Encoding Auto-Select). It
implements the required neuristic which is built-in into IE7 by
default. It has other drawbacks though (search this group for "Korean
issue in IE" for a sample) so I wouldn't suggest to have it activated
permanently: at least until all Web developers will learn to always
indicate encoding for their pages and to match the declared encoding to
the actual one. The achievement of this "halliloya" should be placed to
some very far feature, I'm affraid :-( :-)

P.S. To make what I said more "visual":
1) Imagine you see a string "wninfpevcg" and I tell you that this is
ROT13 encoding. You go to any ROT13 decoder and you get the string
"javascript". No problem.
2) Now imagine you would misinform you by telling that this is ROT47.
You go to a ROT47 decoder and you are getting an obscure sctring
"H?:?7A6G48"

The case 2) is your case, where encoded "javascript" string is your
web-page, bad guy telling a wrong encoding is yourselve (or your server
admin) and "H?:?7A6G48" instead of "javascript" is the result of not
telling the true.
Nov 8 '06 #11
"Logos" <ty*********@gmail.comwrote in news:1162991329.697205.145740
@m73g2000cwd.googlegroups.com:
>
Jim Land (NO SPAM) wrote:
>"Logos" <ty*********@gmail.comwrote in news:1162912848.664818.170200
@m73g2000cwd.googlegroups.com:
For testing, I was using eval("json =" +response.text).

Try eval('"json =" +response.text') and see if that makes a
difference.
>
I don't think that will work - won't that put a string on the left hand
side of an assignment statement, rather than a variable name?
No. It will give eval() something valid to work with.

eval("abc" + "def") errors. So does eval("abc" + myVar). But eval('
"abc" + "def" ') is valid. And eval(' "abc" + myVar ') is valid too.
IE6 and FF1.5.
Nov 8 '06 #12
VK
<OT>
Logos wrote:
Got it in one, Mister! You are my hero, and are awarded the official
title of Javascript GOD.
May I worship at your toesie-wosies? Please? <grin>
Wow! :-) Thank you for your thank you, as they say in England.

</OT>

Nov 8 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
8
by: Bri | last post by:
Greetings, I am using Eval() in a query with only limited success. If the text within the function contains a reference to a Field I get #ERROR#. I'll try and explain what I'm trying to do and...
14
by: ray | last post by:
I am calling a function using eval. It runs in a form module, in response to a button OnClick. strCall = "Forms!frmSchedule.eval" & strSName(0) & "()" On Error Resume Next n = Eval(strCall) ...
4
by: niko.nyman | last post by:
I noticed weird results when using eval() to do some simple calculations. Here are a few eval() statements and the corresponding results from JavaScript: eval("1.2+0.97") =2.17 eval("1.2+0.98")...
8
by: sonet | last post by:
Many Language have eval function.The C have eval function? How to execute c code in c ? Can i execute c code from a variable(like perl's code ref)? Or i must do this job by using yacc & lex ? All...
1
by: christianlott1 | last post by:
I have form code that declares a public recordset at the top: Public pubRS As DAO.Recordset I open a recordset and assign it: Set pubRS = rsSelect (printing current record from pubRS...
12
by: Bill Mill | last post by:
Hello all, I want to have a user able to eval code in a text box. However, if he accidentally types "while(1) { i=0; }" and hits "run", I also want him to be able to hit a stop button such that...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.