unicode/ascii codes in javascript

R

Randell D.

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
 
D

Douglas Crockford

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...

ASCII is a subset of Unicode. Buy a better book.
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?

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
 
R

Randell D.

Douglas said:
ASCII is a subset of Unicode. Buy a better book.



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


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
 
R

Randell D.

Douglas said:
ASCII is a subset of Unicode. Buy a better book.



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

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.
 
R

Randy Webb

Randell D. wrote:

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.
 
R

Randell D.

Randy said:
Randell D. wrote:




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.

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
 
R

Randy Webb

Randell said:
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).

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 :)
 
R

Randell D.

Randy said:
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 :)


Thanks again...
randelld
 
R

Robert

Randell D. said:
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...

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>
 
R

Randell D.

Robert said:
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>

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

Cheers
Randell D.
 
R

Randell D.

Randy said:
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,

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
 
M

Martin Honnen

Randell D. wrote:

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...

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top