웹마스터 팁

<HTML>
<HEAD>
<TITLE>자바스크립트 - 계산기 예제</TITLE>
</HEAD>
<body>
<center>

<script language="JavaScript">
<!-- Hide all this from non javascript browsers

NaN=0;        // if someone typed characters and not digits we assume the value 0
var stack=0; // used to save the first value
var op='+'; // used to save the operator
var assume_equals=false;  // used to calculate sum when didn't click '='
var nnum=true; // the first number is a new number

function digitpress(val) { // pressed a digit
  if (nnum==false) { // append digit to digits currently on the screen
    document.calc.screen.value = document.calc.screen.value+val;
  }
  else { // starting a new number
    stack = parseFloat(document.calc.screen.value);        // save prev number
    document.calc.screen.value = val;        // show new digit
    nnum=false;                // subsequent numbers get appended
  }
  return(true);
}

function pushop(s) { // pressed an operator (one of +, -, *, / or =)
  if (s=='=' || assume_equals==true) { // we should update total on the screen
    // grab the number on the screen now
    v = parseFloat(document.calc.screen.value);
    // catch division by zero
    if (!(op == '/' && v == 0)) {
      // work out the sum and put the result on the screen
      document.calc.screen.value = eval((stack) + op + v);
      if (s != '='+ ') {        // save the operator and the current total
        op = s;
        stack = parseFloat(document.calc.screen.value);
      }
      else {
        assume_equals = false; // this was an '=' so don't total on next op
      }
    }
    else {        // division by zero so display error message
      document.calc.screen.value = "Error";
      assume_equals = false; // this was an error so don't total on next op
    }
    nnum = true;
  }
  else { // otherwise save the current value and the op
    assume_equals = true; // give total on next op
    stack = parseFloat(document.calc.screen.value);        // save current total
    nnum=true;        // next number to be entered is a new one
    op=s;        // remember the operator just typed
  }
  return(true);
}

// End of all the hidden stuff -->
</script>

<FORM name=calc METHOD="POST">

<TABLE border=1 align=center>
<tr align=center><td colspan=5 align=center><INPUT TYPE="float" NAME="screen" VALUE="0" SIZE=20,1 MAXLENGTH=20>

<tr align=center>
<TD>  <INPUT TYPE="button" NAME="one" VALUE=" 1 " onClick="digitpress(1)">
<TD>  <INPUT TYPE="button" NAME="two" VALUE=" 2 " onClick="digitpress(2)">
<TD>  <INPUT TYPE="button" NAME="three" VALUE=" 3 " onClick="digitpress(3)">
<TD>  <input type="button" name="plus" value=" + " onClick="pushop('+')">
<TD>  <input type="button" name="clear" value=" 계산! " onClick="document.calc.screen.value=0;if (nnum==true) nnum = false;">

<TR align=center>
<td><INPUT TYPE="button" NAME="four" VALUE=" 4 " onClick="digitpress(4)">
<td><INPUT TYPE="button" NAME="five" VALUE=" 5 " onClick="digitpress(5)">
<td><INPUT TYPE="button" NAME="six" VALUE=" 6 " onClick="digitpress(6)">
<td><input type="button" name="minus" value=" - " onClick="pushop('-')">
                        <td></td>


<tr align=center>
<td><INPUT TYPE="button" NAME="seven" VALUE=" 7 " onClick="digitpress(7)">
<td><INPUT TYPE="button" NAME="eight" VALUE=" 8 " onClick="digitpress(8)">
<td><INPUT TYPE="button" NAME="nine" VALUE=" 9 " onClick="digitpress(9)">
<td><input type="button" name="times" value=" * " onClick="pushop('*')">
                            <td></td>



<tr align=center>
<td><INPUT TYPE="button" NAME="zero" VALUE=" 0 " onClick="digitpress(0)">
<td><INPUT TYPE="button" NAME="dot" VALUE=" . " onClick="digitpress('.'+ ')">
<TD><input type="button" name="equal" value=" = " onClick="pushop('=')">
<td><input type="button" name="divide" value=" / " onClick="pushop('/')">
                                <td></td>


</TABLE>

</FORM>
</body>
</html>
제목 글쓴이 날짜
텍스트 폼 / 전체선택 하기 자바스크립 mnemosyne 2002.12.24
색다른 링크법(새창) [2] BIRDY™ 2002.12.30
mid 랜덤으로 듣기 ... [2] 아벨라 2003.01.02
숫자를 한글로 변환하는 함수 [1] 행복한고니 2003.01.03
크롬리스에 응용할만한 소스;; [3] MYMob.INT. 2003.01.05
갤러리스킨에 쓰면 좋을 것 같은 미리보기(수정2) [10] 행복한고니 2003.01.06
[re] select form 으로 새 창 띄우기(간단버전) [3] file RedEye 2003.01.27
select form 으로 새 창 띄우기 [4] 당근당근 2003.01.07
홈페이지 패스워드(암호) 걸기 소스 [6] keymove 2003.01.09
혹시 이런것도 될까-_-;;. 시노부 플레이어에서 랜덤 모드 사용자가 택하게 하기 TuTy 2003.01.09
링크가 걸려있는 모든 이미지에 마우스 올리면 서서히 밝아지게 하는 소스입니다. [3] 정해식 2003.01.09
자바스크립트용 계산기 v1.0 [4] 찐군 2003.01.09
[1분짜리 팁!] 홈페이지 입장 여부 묻는 폼 띄우기! [2] 찐군 2003.01.09
IE6SP1 에서 작동하는 크롬리스윈도우!![ByKlein_Chromeless_Window1.0] [13] 술도짱 2003.01.10
1 분마다 배경이 빠뀌는 소스...입니다. [1] q333 2003.01.12
디지털 시계입니다. [8] q333 2003.01.12
성인인증 받는 소스입니다..... [17] q333 2003.01.12
[레드-자바 스크립트초보] 더블클릭하면 경고창 뜬후 사이트 이동하기 [7] 레드 2003.01.17
텍스트 폼에 커서가 미리 깜빡이도록... [8] 카리 2003.01.22
로그인과 로그아웃을 체크하는 방법입니다. [3] 이영호 2003.01.24