2011-05-09 6 views
0

콘솔 출력을 거의 실시간으로 표시하는 페이지를 만들려고합니다. 많은 것을 시도했지만 그 중 아무 것도 작동하지 않는 것처럼 보입니다. 여기에 최신 코드가 있습니다. Ajax는 페이지 업데이트에서만 작동하지 않습니까?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<html> 
<head> 
<title>CP</title> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
function update() { 
    $.ajax({ 
     url: "read.php", 
     cache: false, 
     success: function(html){    
      if(html == ohtml) { 
       alert(html+" ---- "+ohtml); 
      } else { 
       alert(html+" ---- "+ohtml); 
       var ohtml = html; 
       $("#consoleOP").append(html+"<br />"); 
      } 
     } 
    }); 
} 

</script> 
</head> 
<body> 
    <div style='width: 800px; margin-left: auto; margin-right: auto;'><p> 
    <input type="button" value="Refresh" onclick="update()" /></p> 
<div id="consoleOP"></div> 
</div> 
</body> 
</html> 

파일 'read.php'

은 콘솔 로그의 마지막 줄은, 아약스가 페이지를 요청하고 같은 라인에도 불구하고, 사업부는 클릭 것마다에 추가 출력합니다. 새 줄만 표시하고 중복 줄 표시하지 않습니다.

경고가 실행되면 alert()은 'HTMLHTMLHTMLHTML ---- undefined'를 출력합니다.

감사합니다. 저스틴

+0

코드에서 ohtml은 어디에 정의되어 있습니까? –

답변

1
else { 
      alert(html+" ---- "+ohtml); 
      var ohtml = html; <-- I am local and you are treating me as a global 
      $("#consoleOP").append(html+"<br />"); 
     } 

그래서 당신이 범위에서 작동 할 필요가 언급 출력을 받고, 그래서 자바 스크립트에 정의되지 않은 할당

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<html> 
<head> 
<title>CP</title> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 


jQuery(function(){ 

    var ohtml = null; 

    function update() { 
     $.ajax({ 
      url: "read.php", 
      cache: false, 
      success: function(html){    
       if(html == ohtml) { 
        alert(html+" ---- "+ohtml); 
       } else { 
        alert(html+" ---- "+ohtml); 
        ohtml = html; 
        $("#consoleOP").append(html+"<br />"); 
       } 
      } 
     }); 
    } 

    jQuery("#btnRefresh").click(update); 

}); 

</script> 
</head> 
<body> 
    <div style='width: 800px; margin-left: auto; margin-right: auto;'><p> 
    <input type="button" id="btnRefresh" value="Refresh" /></p> 
<div id="consoleOP"></div> 
</div> 
</body> 
</html> 
+0

그 일을했습니다! 감사! – Justin

0

난 당신이 ohtml 변수를 초기화하는 놓친 것 같은데, 당신은

+0

아직도 그랬다 ... – Justin

관련 문제