473,666 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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":"cli entRecords"
,"strAccountId" :"100"
,"strName":" Dr. Gary A. Martin"
,"intReportsPur chased":100
,"intReportsUse d":13
,"intBalance":8 7
,"clientData ":{
"r19":{
"className":"cl ient"
,"intId":353
,"strFname":"Na meOf"
,"strLname":"So meone"
,"strMname": ""
}
,"r7":{
"className":"cl ient"
,"intId":251
,"strFname":"Ba nanaHead"
,"strLname":"My rbø"
,"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 3783
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*********@gm ail.comwrote in news:1162912848 .664818.170200
@m73g2000cwd.go oglegroups.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":"cli entRecords"
,"strAccountId" :"100"
,"strName":" Dr. Gary A. Martin"
,"intReportsPur chased":100
,"intReportsUse d":13
,"intBalance":8 7
,"clientData ":{
"r19":{
"className":"cl ient"
,"intId":353
,"strFname":"Na meOf"
,"strLname":"So meone"
,"strMname": ""
}
,"r7":{
"className":"cl ient"
,"intId":251
,"strFname":"Ba nanaHead"
,"strLname":"My rbø"
,"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 "Unterminat ed 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 "beautifyin g 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>Untitl ed 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>Untitl ed 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 "Unterminat ed 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 "beautifyin g 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>Untitl ed 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*********@gm ail.comwrote in news:1162912848 .664818.170200
@m73g2000cwd.go oglegroups.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

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

Similar topics

9
8459
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 values, this one is a simplified version where its passed. function checkIt(selVal){ valueInDec1 = eval(selVal); //do some calculations here with valueInDec1 }
7
4102
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 the script and special attention was thrown at the fact the script used eval alot. I don't use eval alot in my scripts - but I do use it - and since I always out to learn more / improve my javascript skills, I'm curious why something I thought...
11
1871
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 the use of eval() is required to execute it?" Given the following code, I'm able to evaluate/execute most expressions like: "a.b.c.d()"
8
8617
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 maybe one of you can either tell me how to get the Eval() to work or suggest and alternate method. I have a table called Units: Fields: Unit, Formula Sample Data:
14
4070
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) For the purposes of testing, strSName(0) is always = "LoanType"
4
2105
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") =2.1799999999999997 eval("1.2+0.99") =2.19 eval("1.2+0.981") =2.181 This can happen with other numbers as well, but certainly not with any
8
13280
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 of the non-interpreted languages have no eval function?
1
2924
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 works here)
12
3470
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 his browser does not go into an infinite, soul-crushing, interface-locking loop. The stop button would not need to be instantly responsive, but of course the more responsive the better. Short of writing a javascript-in-javascript interpreter,...
10
494
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 the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following:
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.