2013-03-05 1 views
-1

이 모든 것을 load() 본문 이벤트에 넣으면 페이지가 렌더링되고 DOM 객체가 만들어진 후에로드해야합니까? (나는 그것을 사용하기 위해 .value보다는 .innerhtml을 사용해야 만했다.) 그렇다면 어떻게 ...모든 자바 스크립트를 body 이벤트의 load() 안에 넣어야합니까?

(* 나는 이것이 쓰레기 코드라는 것을 알고 있지만 그것은 나의 마지막 시도보다 낫다. 내 다음 시도. 내가 컴백 시간이 좀되면 내부 함수를 가진 리터럴 생성자를 사용하여 다시 만들 것이다. 나는이 자바 스크립트를 더 이상 사용하지 않을 것이다. 백엔드 PHP는 보안과 체크를 처리 할 것이다.)

<script type="text/javascript"> 

//To get the price of a product applying discount 
function getPrice(location,quantity) 
{ 
    //Get the product choice 
    var e = document.getElementById("productList["+location+"]"); 
    var productSelected = e.options[e.selectedIndex].value; 

    //TODO: Determine discounts based on product choice 
    switch(productSelected) 
    { 
     case '0': 
      return 0; 
     case '1': 
      return 10; 
     case '2': 
      return 15; 
    } 
    return null; 
} 

//To update product only 
function updateProduct(location) 
{ 
    updateRow(location,document.getElementById("quantity["+location+"]").value); 
} 

//To update only that row 
function updateRow(location,quantity) 
{ 
    //Check Quantity is a valid Number and also not a float or negative 
    if (!isNaN(quantity) && parseFloat(quantity) && isFinite(quantity) && (quantity >= 0)) { 
     quantity = Math.floor(quantity); 
    } else { 
     quantity = 0; 
    }; 

    //Update the quantity input field to whatever quantity is - Investigate later! 
    document.getElementById("quantity["+location+"]").value = quantity; 

    //Store old Price for changing total 
    var oldTotal = document.getElementById("totalPrice").innerHTML; 
    var oldLinePrice = document.getElementById("linePrice["+location+"]").innerHTML; 

    //Calculate and Store Prices to adjust the total 
    var productPrice = getPrice(location,quantity).toFixed(2); 
    var newLinePrice = (quantity*productPrice).toFixed(2); 

    //Apply Updates 
    document.getElementById("unitPrice["+location+"]").innerHTML = productPrice; 
    document.getElementById("linePrice["+location+"]").innerHTML = newLinePrice; 
    document.getElementById("totalPrice").innerHTML = (oldTotal-oldLinePrice+parseFloat(newLinePrice)).toFixed(2); 
} 
</script> 
+1

DOM을로드 한 후 실행해야하는 코드는로드 함수에 있어야합니다. 나는 이것이 동어체 간파라는 것을 알지만 다른 대답은 무엇을 기대합니까? – asawyer

+0

이것들은 단지 함수 정의 일 뿐이므로 원하는 곳마다 코드에 넣을 수 있습니다. 페이지가로드 된 후 호출하는 것을 잊지 마십시오. – d4rkpr1nc3

+0

@ d4rkpr1nc3 그게 뭔지는 모르겠지만 DOM을 요소로 사용하지 않아서 생각한 .value보다는 일부 장소에서만 .innerHTML을 사용할 수 있습니다. –

답변

1

에만로드 기능에, 요소를 얻을 기능에 대한 getElement 전화, 또는 전화를 넣어해야합니다.

+0

거의 모든 함수에 'getElementById'가 있기 때문에이 모든 것을로드 함수에 추가해야합니다. –

+0

@WilliamJames onload 스타일 함수 내에 함수 자체를 정의 할 필요가 없습니다 , dom을로드 한 후에 호출하면됩니다. – asawyer

+0

@asawyer 알겠습니다. 나는 그것이 합리적이라고 생각한다. 이제는 왜 당황스럽고 일부 장소에서는 .innerHTML 대신 .value를 사용할 수 없습니다. –

-3

글쎄, 그럴 수도 있겠지만, 처음에는로드 된 .html 코딩 부분에서 작업을 시작하는 일부 js 코드가 지연 될 수 있습니다. 또한 코드가 필요한만큼 들여 쓰기되어 가독성 문제가 발생합니다.

+0

아니요 - 표시된 코드는로드되지 않은 (로드 할 때) 함수의 콜렉션 일 뿐이며 페이지가로드 될 때 실행되는 'load' 이벤트에 실제 함수가 추가되지 않습니다. 이벤트는 소기의 기능을 저장하는 것이 아니라 의도 한 용도로 사용해야합니다. – Paul

관련 문제