Character Codes and Variables 
Author Message
 Character Codes and Variables

I am attempting to make a script using javascript which converts between
characters and character codes (for example, < represents < ). However,
because the character entered into the HTML form is referenced as a variable
in javascript, the code is not displayed as the char, javascript simply
displays the code. It seems to be treating the character code as a string.
How do I tell javascript to treat the character code as if it not a
variable?
--
Nathan Sokalski

http://www.*-*-*.com/


Wed, 25 Feb 2004 08:07:34 GMT  
 Character Codes and Variables
You seem to be doing things the hard way, there should be existing
methods that do what you want, for exampe look at:
("value" is an array)

      for (i=0; i<value.length; i++){      // step each character in string
        ch = value.charAt(i);              // get the current character
        if(ch < " ") {                     // if a non-printable character
          Response.Write("&#26;");         // show box to represent char
          charcode = value.charCodeAt(i);  // calculate decimal equivalent
          Response.Write(charcode);        // show decimal character code
          EOL = true;                      // set flag that near end of line
        }
        else {                             // otherwise it must be printable
          if(EOL == true){                 // it is start of a new line if
            Response.Write("<BR>" + ch);   // the last char was non printable
            EOL = false;                   // reset end of line flag
          }
          else {
            Response.Write(ch);            // nothing unusual, just write char
          }
        }

Which is from a chunk of asp code I wrote to display all the HTTP
server variables (using Jscript):

<%
  // ASP Listing of HTTP Server Variables
  var i;
  var j;
  var EOL;
  var ch;
  var charcode;
  var name = new Array();
  var value = new Array();
  var maxindex;

  http = new Enumerator(Request.ServerVariables);      // access the "collection"
  for(http, i=0; !http.atEnd(); http.moveNext(), i++){ // step through name-value pairs
    name[i] = http.item();                             // create name array
    value[i] = String(Request.ServerVariables(http.item()));  // create value array
    maxindex = i;
  }
  Response.Write("<FONT FACE=\"VERDANA,ARIAL,HELVETICA\">");
  Response.Write("<TABLE BORDER=0 CELLPADDING=2<TR><TD><FONT SIZE=2><B>ServerVariable Name</B></TD><TD><FONT
SIZE=2><B>ServerVariable Value</B></TD><TR>");
  for(i=0; i<=maxindex; i++) {
    if((value[i] != "") && (name[i] != "ALL_HTTP") && (name[i] != "ALL_RAW")){
      Response.Write("<TR><TD><FONT SIZE=2>" + name[i] + "</TD><TD><FONT SIZE=2>" + value[i] + "</TD></TR>");
    }
  }
  Response.Write("<TR><TD><FONT SIZE=2>" + "&nbsp;<BR>" + "</TD><TD><FONT SIZE=2>" + "&nbsp;<BR>" + "</TD></TR>");

  for(i=0; i<=maxindex; i++) {
    if((value[i] == "") && (name[i] != "ALL_HTTP") && (name[i] != "ALL_RAW")){
      Response.Write("<TR><TD><FONT SIZE=2>" + name[i] + "</TD><TD><FONT SIZE=2>" + "NULL (empty string)" + "</TD></TR>");
    }
  }
  Response.Write("<TR><TD><FONT SIZE=2>" + "&nbsp;<BR>" + "</TD><TD><FONT SIZE=2>" + "&nbsp;<BR>" + "</TD></TR>");

  for(j=0; j<2; j++) {
    if(j == 0)
      name = "ALL_RAW";
    else
      name = "ALL_HTTP";
    value = String(Request.ServerVariables(name));// fetch whole string value
    Response.Write("<TR><TD><FONT SIZE=2>" + name + "</TD><TD><FONT SIZE=2>"); // output new table line
    EOL = false;                           // initialize end of line flag (to FALSE)
    if (value == "") {                     // check if value for name is empty,
      Response.Write("&nbsp;");            // if so output a space to create table cell
    }
    else {                                 // if a value so show it:
      for (i=0; i<value.length; i++){      // step each character in string
        ch = value.charAt(i);              // get the current character
        if(ch < " ") {                     // if a non-printable character
          Response.Write("&#26;");         // show box to represent char
          charcode = value.charCodeAt(i);  // calculate decimal equivalent
          Response.Write(charcode);        // show decimal character code
          EOL = true;                      // set flag that near end of line
        }
        else {                             // otherwise it must be printable
          if(EOL == true){                 // it is start of a new line if
            Response.Write("<BR>" + ch);   // the last char was non printable
            EOL = false;                   // reset end of line flag
          }
          else {
            Response.Write(ch);            // nothing unusual, just write char
          }
        }
      }
    }
    Response.Write("<BR></TD></TR>");      // end of the table
  }
  Response.Write("</TABLE>");
%>

Quote:

>I am attempting to make a script using javascript which converts between
>characters and character codes (for example, &#060 represents < ). However,
>because the character entered into the HTML form is referenced as a variable
>in javascript, the code is not displayed as the char, javascript simply
>displays the code. It seems to be treating the character code as a string.
>How do I tell javascript to treat the character code as if it not a
>variable?

---



Wed, 25 Feb 2004 08:58:03 GMT  
 Character Codes and Variables
I think you misunderstood what I wanted. I know how to write javascript that
will create a new page containing character codes in the &#xxx format by use
of the document.write() method, but I do not want to create a new HTML
document. I have a form containing two text fields and two buttons. I want
to be able to enter the character code in one field and have the character
displayed in the other, or vice versa. The problem, from what I have
concluded, is that when using the data entered in a text field (by referring
to it as window.document.formname.fieldname.value), the data is considered
by javascript to be of type string. Therefore, when I assign that value to
either a variable or directly to the other text field, it simply shows up as
&#xxx (with the xxx replaced with whatever I originally entered) instead of
converting it to a character.
--
Nathan Sokalski

http://studentwebs.kutztown.edu/soka0908/HomePage.html


Quote:
> You seem to be doing things the hard way, there should be existing
> methods that do what you want, for exampe look at:
> ("value" is an array)

>       for (i=0; i<value.length; i++){      // step each character in
string
>         ch = value.charAt(i);              // get the current character
>         if(ch < " ") {                     // if a non-printable character
>           Response.Write(" ");         // show box to represent char
>           charcode = value.charCodeAt(i);  // calculate decimal equivalent
>           Response.Write(charcode);        // show decimal character code
>           EOL = true;                      // set flag that near end of
line
>         }
>         else {                             // otherwise it must be
printable
>           if(EOL == true){                 // it is start of a new line if
>             Response.Write("<BR>" + ch);   // the last char was non
printable
>             EOL = false;                   // reset end of line flag
>           }
>           else {
>             Response.Write(ch);            // nothing unusual, just write
char
>           }
>         }

> Which is from a chunk of asp code I wrote to display all the HTTP
> server variables (using Jscript):

> <%
>   // ASP Listing of HTTP Server Variables
>   var i;
>   var j;
>   var EOL;
>   var ch;
>   var charcode;
>   var name = new Array();
>   var value = new Array();
>   var maxindex;

>   http = new Enumerator(Request.ServerVariables);      // access the
"collection"
>   for(http, i=0; !http.atEnd(); http.moveNext(), i++){ // step through
name-value pairs
>     name[i] = http.item();                             // create name
array
>     value[i] = String(Request.ServerVariables(http.item()));  // create
value array
>     maxindex = i;
>   }
>   Response.Write("<FONT FACE=\"VERDANA,ARIAL,HELVETICA\">");
>   Response.Write("<TABLE BORDER=0 CELLPADDING=2<TR><TD><FONT

SIZE=2><B>ServerVariable Name</B></TD><TD><FONT
Quote:
> SIZE=2><B>ServerVariable Value</B></TD><TR>");
>   for(i=0; i<=maxindex; i++) {
>     if((value[i] != "") && (name[i] != "ALL_HTTP") && (name[i] !=
"ALL_RAW")){
>       Response.Write("<TR><TD><FONT SIZE=2>" + name[i] + "</TD><TD><FONT

SIZE=2>" + value[i] + "</TD></TR>");
Quote:
>     }
>   }
>   Response.Write("<TR><TD><FONT SIZE=2>" + "?<BR>" + "</TD><TD><FONT

SIZE=2>" + "?<BR>" + "</TD></TR>");
Quote:

>   for(i=0; i<=maxindex; i++) {
>     if((value[i] == "") && (name[i] != "ALL_HTTP") && (name[i] !=
"ALL_RAW")){
>       Response.Write("<TR><TD><FONT SIZE=2>" + name[i] + "</TD><TD><FONT

SIZE=2>" + "NULL (empty string)" + "</TD></TR>");
Quote:
>     }
>   }
>   Response.Write("<TR><TD><FONT SIZE=2>" + "?<BR>" + "</TD><TD><FONT

SIZE=2>" + "?<BR>" + "</TD></TR>");
Quote:

>   for(j=0; j<2; j++) {
>     if(j == 0)
>       name = "ALL_RAW";
>     else
>       name = "ALL_HTTP";
>     value = String(Request.ServerVariables(name));// fetch whole string
value
>     Response.Write("<TR><TD><FONT SIZE=2>" + name + "</TD><TD><FONT

SIZE=2>"); // output new table line

- Show quoted text -

Quote:
>     EOL = false;                           // initialize end of line flag
(to FALSE)
>     if (value == "") {                     // check if value for name is
empty,
>       Response.Write("?");            // if so output a space to
create table cell
>     }
>     else {                                 // if a value so show it:
>       for (i=0; i<value.length; i++){      // step each character in
string
>         ch = value.charAt(i);              // get the current character
>         if(ch < " ") {                     // if a non-printable character
>           Response.Write(" ");         // show box to represent char
>           charcode = value.charCodeAt(i);  // calculate decimal equivalent
>           Response.Write(charcode);        // show decimal character code
>           EOL = true;                      // set flag that near end of
line
>         }
>         else {                             // otherwise it must be
printable
>           if(EOL == true){                 // it is start of a new line if
>             Response.Write("<BR>" + ch);   // the last char was non
printable
>             EOL = false;                   // reset end of line flag
>           }
>           else {
>             Response.Write(ch);            // nothing unusual, just write
char
>           }
>         }
>       }
>     }
>     Response.Write("<BR></TD></TR>");      // end of the table
>   }
>   Response.Write("</TABLE>");
> %>

> On Fri, 7 Sep 2001 20:07:34 -0400, "Nathan Sokalski"

> >I am attempting to make a script using javascript which converts between
> >characters and character codes (for example, < represents < ).
However,
> >because the character entered into the HTML form is referenced as a
variable
> >in javascript, the code is not displayed as the char, javascript simply
> >displays the code. It seems to be treating the character code as a
string.
> >How do I tell javascript to treat the character code as if it not a
> >variable?

> ---




Wed, 25 Feb 2004 11:52:08 GMT  
 Character Codes and Variables
<html>
<script language="JavaScript">
function foo(){
  b.innerHTML = a.value;
  alert(a.value+" is "+b.innerText);
Quote:
}

</script>
<body language="JavaScript">
<input id=a type="text" value="&amp;#60;">
<span id=b style="display:none;"></span>
<input type=button onclick="foo()" value="go">
</form>
</body>
</html>

If you want the opposite effect, then assign the character value to b.innerText and read back from
b.innerHTML...

--

Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

Quote:

> I am attempting to make a script using javascript which converts between
> characters and character codes (for example, &#060 represents < ). However,
> because the character entered into the HTML form is referenced as a variable
> in javascript, the code is not displayed as the char, javascript simply
> displays the code. It seems to be treating the character code as a string.
> How do I tell javascript to treat the character code as if it not a
> variable?
> --
> Nathan Sokalski

> http://studentwebs.kutztown.edu/soka0908/HomePage.html



Wed, 25 Feb 2004 12:54:12 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. special characters and ASCII code

2. Extracting ASCII code from Characters

3. Character code!

4. ANSI Character Codes to String in JScript

5. ASCII Character Code Reference Chart

6. Accessing a character by its numeric code

7. hex code to character conversion

8. NON US Characters in vbscript code gives an error when registering a .wsc file

9. Ascii Coded Characters in JavaScripts and QueryStrings

10. character codes

11. Value single line text box with variable containing special characters

12. How many characters long may a variable be ?

 

 
Powered by phpBB® Forum Software