JavaScript DHTML/Language Basics/Char

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

Character Conversions

   <source lang="html4strict">

<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> Capturing Character Codes <FORM>

Select any of this text, and see the character code of the first character.

Character Code:<INPUT TYPE="text" NAME="charCodeDisplay" SIZE=3>


</FORM> </BODY> </HTML>


      </source>
   
  


Defining Special Characters as Variables for Later Use

   <source lang="html4strict">

/* Symbol Description \t Tab \n Newline \r Carriage return \f Form feed \\ Backslash \b Backspace \" Double quote \" Single quote

  • /

<script type="text/javascript">

</script>


      </source>
   
  


Displaying a String Containing Escaped Quotations

   <source lang="html4strict">

<HTML> <BODY>

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

</BODY> </HTML>

      </source>
   
  


Using Special Characters in JavaScript

   <source lang="html4strict">
   \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>

    <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>
  
</body>

</html>

      </source>
   
  


Using Special Formatting Characters

   <source lang="html4strict">

<HTML> <HEAD> <TITLE>Using special formatting characters</TITLE> </HEAD> <BODY>

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

</BODY> </HTML>

</source>