2017-05-22 8 views
-1

C에서이 코드 예제를 사용하여 위의 오류가 발생했습니다. 사용 된 함수는 src.js에 있습니다. 나는 웹 UI를로드 할 때 오류가 잘 된 SomeFuncion()하지만 anotherFunction()에 대한 작품 팝업 :Onload not working : 잡히지 않은 참조 오류, 정의되지 않은 함수

fprintf(out, "<script type='text/javascript' src='/src.js'></script>\n"); 
fprintf(out, "<body onload=\"someFunction()\">\n"); 
fprintf(out, "<button type='button' id='CheckButton' onclick='anotherFunction()'>Check</button></br>"); 

src.js :

var check = 0; 
function someFunction() 
{ 
    req=new XMLHttpRequest(); 
    req.onreadystatechange=yetAnotherFunction; 
    req.open("GET","xxx",true); 
    req.send(); 
    setTimeout("someFunction()", 3000); 
} 

function anotherFunction() 
{ 
    if (!check) { 
    check = 1; 
    setButtonText('CheckButton', 'Stop Checking'); 
    someFunction(); 
    } else { 
    check = 0; 
    setButtonText('CheckButton', 'Checking'); 
    } 
} 

가 난 할 노력하고있어하면 제거입니다 버튼을 클릭하고 onload 기능으로 바꿉니다. 누구든지이 코드의 문제점을 알고 있습니까?

+0

무엇이'function()'입니까? –

+0

function()이 src.js에 정의되어 있는지 확인하십시오. src.js가 html과 동일한 디렉토리에 있으면 src = "src.js"만 사용하십시오. – Ollaw

+1

'function'은 예약어입니다. 당신은'function'이라는 함수를 가질 수 없습니다. – rustyx

답변

0

자바 스크립트에서 function은 기능을 만드는 데 사용되는 키워드입니다. 함수의 이름으로 사용할 수 없습니다. someFunction()에서 fprintf(out, "<body onload=\"someFunction()\">\n");

로 전화하여 src.js, 변경 된 SomeFuncion에서

function someFunction() { 

    console.log("someFunction") 
    req = new XMLHttpRequest(); 
    req.onreadystatechange = yetAnotherFunction; 
    req.open("GET", "xxx", true); 
    req.send(); 
    setTimeout(someFunction, 3000); // <== this is wrong 
} 

에, 당신은하지 문자열, setTimeout에 매개 변수로 함수를 통과해야합니다.

+0

그냥 예제 그래서 더 명확하게 질문을 편집 – purry

+1

'src.js' 및 실제로'src.js' 위치를 추가하십시오 감사합니까? 루트 디렉토리'/'에? –

+0

src.js는 c 파일과 동일한 디렉토리에 있습니다 – purry

관련 문제