Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

NaN” is not a string, nor is it a data type of its own. It is primarily a number! You can prove that to yourself via the following statement:

alert(typeof parseInt("something"))

The following construct demonstrates how to implement the isNaN function (with the parseFloat() function for the sake of the example):

var floatValue = parseFloat(valueToBeConvertedToFloat)

if isNaN(floatValue) {
   functionToBeCalledIfFloat()
} else {
   functionToBeCalledIfNotFloat()
}

The isNaN() function is not as important as parseInt and parseFloat, but we have discussed it here for completeness.

Evaluating Text Expressions

JavaScript supports evaluation and execution of text expressions via the eval() method. Here are some examples:

var str = "5 + 2"
var num = eval(str)
alert(num)

var al = "alert('This is an evaluated string.')"
eval(al)

This script segment pops up two alerts. The first one displays the number 7, because the expression “5 + 2” evaluates to 7. The second call to the eval() function does not cause it to return a value, but to execute the statement encapsulated in a string.

You can also use the eval() function to convert strings representing numbers to regular numbers.

The eval() function accepts any valid JavaScript piece of code in the form of a string. You can store an entire script as a string and then hand it over to this function. The classic example for the function is to let the user enter a mathematical expression in a form field or a prompt box, and to display the result. Here is a simple example:

var inp = prompt("Enter mathematical expression", "")
alert(inp + " = " + eval(inp))

String Handling Examples

In this section we focus on scripts that take advantage of the various built-in functions as well as the elements of the String object in JavaScript.

String Enciphering

The following script prompts the user for a short string. It then asks for a numeric key. The key has 63 possible values—all integers from 1 to 63. The ciphering technique used in this script is known as XORing, because it is primarily based on the bitwise XOR (exclusive OR) operator. The numeric value of each character of the input string, or password, is mixed with the numeric key value. A reversed process can be used to convert the enciphered string back to the original one. Since the conversion simply swaps the same two characters according to the key, the same JavaScript script is used as the decoder and the encoder. Enough theory—let’s get to the point! Here is the script:

<HTML>
<HEAD>
<TITLE>Enciphering</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--

// create list of valid characters
var list = "0123456789abcdefghijklmnopqrstuvwxyz._~ABCDEFGHIJKLMNO
  PQRSTUVWXYZ"

function encipher() {
 // prompt user for string
 var str = prompt("Enter string:", "")

 // terminate function if user selects CANCEL
 if (!str)
  return

 // check that each character of input string is valid
 for (var i = 0; i < str.length; ++i) {
  if (list.indexOf(str.charAt(i)) == –1) {
   alert("script terminated -- invalid character found")
   return
  }
 }

 // prompt user for key
 var key = prompt("Enter key (1-63):", "")

 // terminate function if user selects CANCEL
 if (!key)
  return

 // convert key to integer (number)
 key = parseInt(key)

 // alert enciphered string
 alert(encode(str, key))
}

function encode(str, key) {
 // initialize accumulative string variable
 var code = ""

 // encipher all characters
 for (var i = 0; i < str.length; ++i) {
  var ind = list.indexOf(str.charAt(i))
  var converted = list.charAt(ind ^ key)
  code += converted
 }

 // return enciphered value
 return code
}

encipher()
// -->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us