JavaScript DHTML/Document/Eval

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

"eval()" Example

 
    
<html>
<body>
<script language="javascript">
function function1(){
   var fullName = "Joe";
   var mySentence = eval(""My name is " + fullName;");
   alert(mySentence); 
}
</script>
<button onclick="function1();">Eval function</button>
</body>
</html>



Use eval function to run statement dynamically

 
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var num1, op, num2, ans; 
num1 = 2;
op = "+";
num2 = 3;
ans = eval(Number(num1)+ op + Number(num2));
alert(num1 + " " + op + " " + num2 + " = " + ans);
</script>
</head>
<body>
</body>
</html>