By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

unicode/ascii codes in javascript

P: n/a

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
Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
> 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
Jul 23 '05 #2

P: n/a
Douglas Crockford wrote:
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

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
Jul 23 '05 #3

P: n/a
Douglas Crockford wrote:
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


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.
Jul 23 '05 #4

P: n/a
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

P: n/a
Randy Webb wrote:
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.


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
Jul 23 '05 #6

P: n/a
Randell D. wrote:
Randy Webb wrote:
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.


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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7

P: n/a
Randy Webb wrote:
Randell D. wrote:
Randy Webb wrote:
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.


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

Thanks again...
randelld
Jul 23 '05 #8

P: n/a
In article <ZWONd.315378$Xk.22008@pd7tw3no>,
"Randell D." <re******************************@fiprojects.moc > 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...


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>
Jul 23 '05 #9

P: n/a
Robert wrote:
In article <ZWONd.315378$Xk.22008@pd7tw3no>,
"Randell D." <re******************************@fiprojects.moc > 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...

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.
Jul 23 '05 #10

P: n/a
Randy Webb wrote:
Randell D. wrote:
Randy Webb wrote:
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.


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

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
Jul 23 '05 #11

P: n/a


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #12

P: n/a
JRS: In article <Fc********************@comcast.com>, dated Mon, 7 Feb
2005 18:01:06, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
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)

Why not with String.fromCharCode() ?

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

-
comp.lang.javascript FAQ - http://jibbering.com/faq


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.
Jul 23 '05 #13

P: n/a
Dr John Stockton wrote:
JRS: In article <Fc********************@comcast.com>, dated Mon, 7 Feb
2005 18:01:06, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :


<--snip-->
comp.lang.javascript FAQ - http://jibbering.com/faq

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


Semi-boilerplate but close enough that I changed it.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.