Connecting Tech Pros Worldwide Help | Site Map

unicode/ascii codes in javascript

Randell D.
Guest
 
Posts: n/a
#1: Jul 23 '05

Folks,

I have two related questions:

1. I have seen unicode being mentioned in my javascript pocket book - is
this the same as ascii codes? I think not though I'm not sure and I
can't find any examples...

2. How do I read/set a string to an unprintable code? For example, how
can I set the variable a to equal the equivalent of ascii code 7 or 8 or
whatever...

Why? I am working on an application which allows the user to enter in
multiple names - I want to store these multiple names in a hidden text
box using a none-printable character as a field seperate for each input
text field value. I know 100% that my keyboard entries will be
ascii/qwerty.

Can someone steer me in the right direction?

randelld
Douglas Crockford
Guest
 
Posts: n/a
#2: Jul 23 '05

re: unicode/ascii codes in javascript


> 1. I have seen unicode being mentioned in my javascript pocket book - is[color=blue]
> this the same as ascii codes? I think not though I'm not sure and I
> can't find any examples...[/color]

ASCII is a subset of Unicode. Buy a better book.
[color=blue]
> 2. How do I read/set a string to an unprintable code? For example, how
> can I set the variable a to equal the equivalent of ascii code 7 or 8 or
> whatever...
>
> Why? I am working on an application which allows the user to enter in
> multiple names - I want to store these multiple names in a hidden text
> box using a none-printable character as a field seperate for each input
> text field value. I know 100% that my keyboard entries will be
> ascii/qwerty.
>
> Can someone steer me in the right direction?[/color]

Yeah. Don't do it. No security is obtained from such ridiculous methods.
The list of names must be kept on the server, never in the client.

http://www.crockford.com/javascript/survey.html
Randell D.
Guest
 
Posts: n/a
#3: Jul 23 '05

re: unicode/ascii codes in javascript


Douglas Crockford wrote:
[color=blue][color=green]
>> 1. I have seen unicode being mentioned in my javascript pocket book -
>> is this the same as ascii codes? I think not though I'm not sure and I
>> can't find any examples...[/color]
>
>
> ASCII is a subset of Unicode. Buy a better book.
>[color=green]
>> 2. How do I read/set a string to an unprintable code? For example,
>> how can I set the variable a to equal the equivalent of ascii code 7
>> or 8 or whatever...
>>
>> Why? I am working on an application which allows the user to enter in
>> multiple names - I want to store these multiple names in a hidden text
>> box using a none-printable character as a field seperate for each
>> input text field value. I know 100% that my keyboard entries will be
>> ascii/qwerty.
>>
>> Can someone steer me in the right direction?[/color]
>
>
> Yeah. Don't do it. No security is obtained from such ridiculous methods.
> The list of names must be kept on the server, never in the client.
>
> http://www.crockford.com/javascript/survey.html[/color]


Thanks - I am aware of security concerns - but security is not a problem
for me as I'm working on a small intranet based application - in
addition, I will sanitize my input using PHP...

Thus - canyou answer my second question? How can I sest a variable to be
the equivalent of ascii code 7 or 8 or whatever - I believe I can read a
string, and find the unicode values of each character, but I don't know
what function I can use for the reverse of this process.

cheers
randelld
Randell D.
Guest
 
Posts: n/a
#4: Jul 23 '05

re: unicode/ascii codes in javascript


Douglas Crockford wrote:[color=blue][color=green]
>> 1. I have seen unicode being mentioned in my javascript pocket book -
>> is this the same as ascii codes? I think not though I'm not sure and I
>> can't find any examples...[/color]
>
>
> ASCII is a subset of Unicode. Buy a better book.
>[color=green]
>> 2. How do I read/set a string to an unprintable code? For example,
>> how can I set the variable a to equal the equivalent of ascii code 7
>> or 8 or whatever...
>>
>> Why? I am working on an application which allows the user to enter in
>> multiple names - I want to store these multiple names in a hidden text
>> box using a none-printable character as a field seperate for each
>> input text field value. I know 100% that my keyboard entries will be
>> ascii/qwerty.
>>
>> Can someone steer me in the right direction?[/color]
>
>
> Yeah. Don't do it. No security is obtained from such ridiculous methods.
> The list of names must be kept on the server, never in the client.
>
> http://www.crockford.com/javascript/survey.html[/color]

Ah! I've just realised that you have have also mis-understood my second
question... I do not want to have the names written as part of the
javascript... this I would agree, would be silly...

I have a form - it has INPUT tags asking for firstname and lastname.

I also have a hidden input box.

When the user enters a firstname and lastname, I have an onClick
function which takes the input from the firstname/lastname box, and
writes it to the hidden input box. I want this so as the client can
enter several names all at once without having to post the form each
time. Once they have finished inputing the names, the user clicks on a
submit button which then passes the hidden input box to my php based server.

In order for my php script to read the input, I want the hidden text box
to have a fixed format - thus, for example, firstname is always followed
by ascii character 8 followed by lastname. I can then easily unpack
this long string on the server side.

Thus - can you tell me how I can set a variable called fs equal to ascii
character 8? Or even unicode character 8? (It doesn't have to be 8 - but
something not readily available from qwerty keyboard input)

Thanks
Randell D.
Randy Webb
Guest
 
Posts: n/a
#5: Jul 23 '05

re: unicode/ascii codes in javascript


Randell D. wrote:

<--snip-->
[color=blue]
> Thus - can you tell me how I can set a variable called fs equal to ascii
> character 8? Or even unicode character 8? (It doesn't have to be 8 - but
> something not readily available from qwerty keyboard input)[/color]

var fs="ß";

Copy and paste as desired. Unless someone you know spells a name with a
B like that. Then, you could use :

var fs="Þ";

or something similar.

It doesn't have to be "non-printable", it only has to be unique.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Randell D.
Guest
 
Posts: n/a
#6: Jul 23 '05

re: unicode/ascii codes in javascript


Randy Webb wrote:[color=blue]
> Randell D. wrote:
>
> <--snip-->
>[color=green]
>> Thus - can you tell me how I can set a variable called fs equal to
>> ascii character 8? Or even unicode character 8? (It doesn't have to be
>> 8 - but something not readily available from qwerty keyboard input)[/color]
>
>
> var fs="ß";
>
> Copy and paste as desired. Unless someone you know spells a name with a
> B like that. Then, you could use :
>
> var fs="Þ";
>
> or something similar.
>
> It doesn't have to be "non-printable", it only has to be unique.
>[/color]

Yep - I agree that it only has to be unique and I'm grateful for your
suggested solution (which I will use) however I guess there is no method
available in javascript to do what I want to do... true? (Just for a
curiosity/js-educational point of view).

thanks
randelld
Randy Webb
Guest
 
Posts: n/a
#7: Jul 23 '05

re: unicode/ascii codes in javascript


Randell D. wrote:
[color=blue]
> Randy Webb wrote:
>[color=green]
>> Randell D. wrote:
>>
>> <--snip-->
>>[color=darkred]
>>> Thus - can you tell me how I can set a variable called fs equal to
>>> ascii character 8? Or even unicode character 8? (It doesn't have to
>>> be 8 - but something not readily available from qwerty keyboard input)[/color]
>>
>>
>>
>> var fs="ß";
>>
>> Copy and paste as desired. Unless someone you know spells a name with
>> a B like that. Then, you could use :
>>
>> var fs="Þ";
>>
>> or something similar.
>>
>> It doesn't have to be "non-printable", it only has to be unique.
>>[/color]
>
> Yep - I agree that it only has to be unique and I'm grateful for your
> suggested solution (which I will use) however I guess there is no method
> available in javascript to do what I want to do... true? (Just for a
> curiosity/js-educational point of view).[/color]

Never really tried but instincts tells me it should be able to, provided
you can get the Unicode character sequence for it.

A while back I wrote a script that I needed a delimiter for much as you
need one and settled on the following:

var myVar = "ßàßÝÞ±º¹²³®©";

And unless someone every happened to type that particular sequence then
my app is pretty secure :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Randell D.
Guest
 
Posts: n/a
#8: Jul 23 '05

re: unicode/ascii codes in javascript


Randy Webb wrote:
[color=blue]
> Randell D. wrote:
>[color=green]
>> Randy Webb wrote:
>>[color=darkred]
>>> Randell D. wrote:
>>>
>>> <--snip-->
>>>
>>>> Thus - can you tell me how I can set a variable called fs equal to
>>>> ascii character 8? Or even unicode character 8? (It doesn't have to
>>>> be 8 - but something not readily available from qwerty keyboard input)
>>>
>>>
>>>
>>>
>>> var fs="ß";
>>>
>>> Copy and paste as desired. Unless someone you know spells a name with
>>> a B like that. Then, you could use :
>>>
>>> var fs="Þ";
>>>
>>> or something similar.
>>>
>>> It doesn't have to be "non-printable", it only has to be unique.
>>>[/color]
>>
>> Yep - I agree that it only has to be unique and I'm grateful for your
>> suggested solution (which I will use) however I guess there is no
>> method available in javascript to do what I want to do... true? (Just
>> for a curiosity/js-educational point of view).[/color]
>
>
> Never really tried but instincts tells me it should be able to, provided
> you can get the Unicode character sequence for it.
>
> A while back I wrote a script that I needed a delimiter for much as you
> need one and settled on the following:
>
> var myVar = "ßàßÝÞ±º¹²³®©";
>
> And unless someone every happened to type that particular sequence then
> my app is pretty secure :)
>[/color]


Thanks again...
randelld
Robert
Guest
 
Posts: n/a
#9: Jul 23 '05

re: unicode/ascii codes in javascript


In article <ZWONd.315378$Xk.22008@pd7tw3no>,
"Randell D." <reply.via.news.group.only.thanks@fiprojects.moc > wrote:
[color=blue]
> 2. How do I read/set a string to an unprintable code? For example, how
> can I set the variable a to equal the equivalent of ascii code 7 or 8 or
> whatever...[/color]

This may help.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Saving stats</title>
<base href="http://spaceplace.jpl.nasa.gov/en/_images/">
<script type="text/javascript">

nullCharacter = "\0"
backSpace = "\b"

orUnicode = "\u0000"
oldChar = "\x00"

alert ( "nullCharacter.length = " + nullCharacter.length + " " +
escape(nullCharacter) +
"\n" +
" backSpace = '" + backSpace + "'" + " " +
escape(backSpace) +
"\n" +
" orUnicode.length = " + orUnicode.length + " " +
escape(orUnicode) +
"\n" +
" oldChar.length = " + oldChar.length + " " +
escape(oldChar))

</script>
</head>
<body >
<p> How is that.</>
</body>
</html>
Randell D.
Guest
 
Posts: n/a
#10: Jul 23 '05

re: unicode/ascii codes in javascript


Robert wrote:
[color=blue]
> In article <ZWONd.315378$Xk.22008@pd7tw3no>,
> "Randell D." <reply.via.news.group.only.thanks@fiprojects.moc > wrote:
>
>[color=green]
>>2. How do I read/set a string to an unprintable code? For example, how
>>can I set the variable a to equal the equivalent of ascii code 7 or 8 or
>>whatever...[/color]
>
>
> This may help.
>
> Robert
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html>
> <head>
> <title>Saving stats</title>
> <base href="http://spaceplace.jpl.nasa.gov/en/_images/">
> <script type="text/javascript">
>
> nullCharacter = "\0"
> backSpace = "\b"
>
> orUnicode = "\u0000"
> oldChar = "\x00"
>
> alert ( "nullCharacter.length = " + nullCharacter.length + " " +
> escape(nullCharacter) +
> "\n" +
> " backSpace = '" + backSpace + "'" + " " +
> escape(backSpace) +
> "\n" +
> " orUnicode.length = " + orUnicode.length + " " +
> escape(orUnicode) +
> "\n" +
> " oldChar.length = " + oldChar.length + " " +
> escape(oldChar))
>
> </script>
> </head>
> <body >
> <p> How is that.</>
> </body>
> </html>[/color]

Interesting... I've saved that for reference... it took me a second, but
I can understand what you've done...

Cheers
Randell D.
Randell D.
Guest
 
Posts: n/a
#11: Jul 23 '05

re: unicode/ascii codes in javascript


Randy Webb wrote:
[color=blue]
> Randell D. wrote:
>[color=green]
>> Randy Webb wrote:
>>[color=darkred]
>>> Randell D. wrote:
>>>
>>> <--snip-->
>>>
>>>> Thus - can you tell me how I can set a variable called fs equal to
>>>> ascii character 8? Or even unicode character 8? (It doesn't have to
>>>> be 8 - but something not readily available from qwerty keyboard input)
>>>
>>>
>>>
>>>
>>> var fs="ß";
>>>
>>> Copy and paste as desired. Unless someone you know spells a name with
>>> a B like that. Then, you could use :
>>>
>>> var fs="Þ";
>>>
>>> or something similar.
>>>
>>> It doesn't have to be "non-printable", it only has to be unique.
>>>[/color]
>>
>> Yep - I agree that it only has to be unique and I'm grateful for your
>> suggested solution (which I will use) however I guess there is no
>> method available in javascript to do what I want to do... true? (Just
>> for a curiosity/js-educational point of view).[/color]
>
>
> Never really tried but instincts tells me it should be able to, provided
> you can get the Unicode character sequence for it.
>
> A while back I wrote a script that I needed a delimiter for much as you
> need one and settled on the following:
>
> var myVar = "ßàßÝÞ±º¹²³®©";
>
> And unless someone every happened to type that particular sequence then
> my app is pretty secure :)
>[/color]


Randy,

I have another post,with subject line 'Re: Odd problem: Data from hidden
field is not sent to server...' that you might be able to help with (if
you don't mind). Without duplicating the entire post, basically, *the
value* my box receives from the result of a javascript function does not
get sent to the server - yet if I manually complete the same field, the
manually entered data does get sent to the server.

I'm wondering if you had any similar problem as you mentioned you've
done something similar to me at some point in the past...

If you can help please reply to my other post as it might well help
others at some pointin the future.

Cheers
randelld
Martin Honnen
Guest
 
Posts: n/a
#12: Jul 23 '05

re: unicode/ascii codes in javascript




Randell D. wrote:

[color=blue]
> 2. How do I read/set a string to an unprintable code? For example, how
> can I set the variable a to equal the equivalent of ascii code 7 or 8 or
> whatever...[/color]

There is a method
String.fromCharCode
which takes one ore more numbers as its arguments and creates a string
with as many characters as arguments where the characters are taken from
the numbers interpreted as Unicode code points, thus
var s = String.fromCharCode(65, 66, 67);
makes s have the value 'ABC' or
var s = String.fromCharCode(8364);
the value '€'.

Check <http://www.unicode.org/> for Unicode code charts.

The other way round is
string.charCodeAt(characterIndex)
e.g. with var s = String.fromCharCode(8364); the expression
s.charCodeAt(0) yields the number 8364.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dr John Stockton
Guest
 
Posts: n/a
#13: Jul 23 '05

re: unicode/ascii codes in javascript


JRS: In article <FcOdneGtn8a4bprfRVn-tw@comcast.com>, dated Mon, 7 Feb
2005 18:01:06, seen in news:comp.lang.javascript, Randy Webb
<HikksNotAtHome@aol.com> posted :[color=blue]
>Randell D. wrote:
>
><--snip-->
>[color=green]
>> Thus - can you tell me how I can set a variable called fs equal to ascii
>> character 8? Or even unicode character 8? (It doesn't have to be 8 - but
>> something not readily available from qwerty keyboard input)[/color][/color]

Why not with String.fromCharCode() ?

S = String.fromCharCode(0x62b) shows me three peas falling into a wok.

-
[color=blue]
>comp.lang.javascript FAQ - http://jibbering.com/faq[/color]

If that's boilerplate, how about adding "& newsgroup weekly"?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Randy Webb
Guest
 
Posts: n/a
#14: Jul 23 '05

re: unicode/ascii codes in javascript


Dr John Stockton wrote:[color=blue]
> JRS: In article <FcOdneGtn8a4bprfRVn-tw@comcast.com>, dated Mon, 7 Feb
> 2005 18:01:06, seen in news:comp.lang.javascript, Randy Webb
> <HikksNotAtHome@aol.com> posted :[/color]

<--snip-->
[color=blue][color=green]
>>comp.lang.javascript FAQ - http://jibbering.com/faq[/color]
>
>
> If that's boilerplate, how about adding "& newsgroup weekly"?[/color]

Semi-boilerplate but close enough that I changed it.


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Closed Thread