Two Methods of addition operation written in Javascript
Code:
- <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
- <HTML>
- <Head>
- <Title> demo01.html </title>
- <Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
- <Meta http-equiv = "Description" content = "this is my page">
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
- <! -- <LINK rel = "stylesheet" type = "text/CSS" href = "./styles.css"> -->
- <SCRIPT type = "text/JavaScript" src = "./JS/demo01.js">
- </SCRIPT>
- <SCRIPT type = "text/JavaScript">
- // Method 1
- Function sum (sums ){
- VaR num1 = Document. getelementbyid ("num1"). value;
- VaR num2 = Document. getelementbyid ("num2"). value;
- VaR num3 = eval (num1 + num2 );
- Document. getelementbyid ("num3"). value = num3;
- }
- // Method 2
- Function sum1 (){
- VaR num1 = Document. getelementbyid ("num1"). value;
- VaR num2 = Document. getelementbyid ("num2"). value;
- If (isnan (num1 )){
- Alert ("the format of the first input box is incorrect. Enter a number of non-character types ");
- } Else {
- If (isnan (num2 )){
- Alert ("the format of the second input box is incorrect. Enter a number of non-character types ");
- } Else {
- VaR num11 = parseint (num1 );
- VaR num22 = parseint (num2 );
- VaR num3 = num11 + num22;
- Document. getelementbyid ("num3"). value = num3;
- }
- }
- }
- </SCRIPT>
- </Head>
- <Body>
- <Input type = "text" name = "num1" id = "num1">-<input type = "text" name = "num2" id = "num2">
- <Input type = "button" value = "=" onclick = "sum ('-')"> <! -- Method 1 -->
- <! -- <Input type = "button" value = "=" onclick = "sum1 ()"> --> <! -- Method 2 -->
- <Input type = "text" name = "num3" id = "num3">
- </Body>
- </Html>