JavaScript DHTML/Language Basics/Char

Материал из Web эксперт
Перейти к: навигация, поиск

Character Conversions

<HTML>
<HEAD>
<TITLE>Character Codes</TITLE>
<SCRIPT LANGUAGE="JavaScript">
var isNav = (navigator.appName == "Netscape")
var isNav4 = (isNav && parseInt(navigator.appVersion == 4))
function showCharCode() {
    if (isNav) {
        var theText = document.getSelection()
    } else {
        var theText = document.selection.createRange().text
    }
    if (theText) {
        document.forms[0].charCodeDisplay.value = theText.charCodeAt()
    } else {
        document.forms[0].charCodeDisplay.value = " "
    }
}
function showString(form) {
    form.result.value = String.fromCharCode(form.entry1.value,
form.entry2.value,form.entry3.value)
}
if (isNav4) {
    document.captureEvents(Event.MOUSEUP)
}
document.onmouseup = showCharCode
</SCRIPT>
</HEAD>
<BODY>
<B>Capturing Character Codes</B>
<FORM>
Select any of this text, and see the character code of the first character.<P>
Character Code:<INPUT TYPE="text" NAME="charCodeDisplay" SIZE=3><BR>
<HR>
</FORM>
</BODY>
</HTML>



Defining Special Characters as Variables for Later Use

/*
Symbol                Description
\t                    Tab
\n                    Newline
\r                    Carriage return
\f                    Form feed
\\                    Backslash
\b                    Backspace
\"                    Double quote
\"                    Single quote
*/
<script type="text/javascript">
<!--
  // Inline Character Constants
  var TAB = "\t";
  var CR = "\r";
  var LF = "\n";
  var CRLF = "\r\n";
  var FF = "\f";
  var DQUOTE = "\"";
  var SQUOTE = "\"";
  var BACKSLASH = "\\";
  var BACKSPACE = "\b";
   
  document.write("Column1" + TAB + "Column2" + TAB + "Column3")
//-->
</script>



Displaying a String Containing Escaped Quotations

<HTML>
<BODY>
<H1>
<SCRIPT> 
var potterSays = "\"I want to read it,\" said Joe, \"as it\"s mine!\""; 
document.write(potterSays); 
</script>
</H1>
</BODY>
</HTML>



Using Special Characters in JavaScript

    \b indicates a backspace.
    \f indicates a form feed.
    \n indicates a new line.
    \r indicates a carriage return.
    \t indicates a tab.
    \\ indicates a backslash.
    \" indicates a single quote.
    \" indicates a double quote.
     Special characters do not take effect unless enclosed in a 
     pre-formatted block 
<html>
<head>
  <title>JavaScript Unleashed</title>
</head>
<body>
  <pre>
    <script type="text/javascript">
    <!--
      document.writeln("\tPersonnel");
      document.writeln("Name\t\tAddress");
      document.writeln("Jeff\t\tjeff@company.ru");
      document.writeln("Bill\t\tbill@company.ru");
      document.writeln("Kim\t\tkim@company.ru");
    // -->
    </script>
  </pre></body>
</html>



Using Special Formatting Characters

<HTML>
<HEAD>
<TITLE>Using special formatting characters</TITLE>
</HEAD>
<BODY>
<PRE>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("This shows how the \bbackspace character works.\n")
document.write("This shows how the \ttab character works.\n")
document.write("This shows how the \rcarriage return character works.\n")
document.write("This shows how the \fform feed character works.\n")
document.write("This shows how the \nnew line character works.\n")
// -->
</SCRIPT>
</PRE>
</BODY>
</HTML>