2014-09-23 6 views
-3

제공된 값에 따라 다른 HTML 파일을 호출하려고합니다. 상기 다른 HTML 페이지는 여기입력을 기반으로 다른 HTML 페이지 호출

<html> 
    <body> 
    <p>This example calls different HTML pages based on the input</p> 
     <p id="htmlinvoke"></p> 
     <script> 
     function loadPage(href) 
        { 
        alert('loadPage'); 
         var xmlhttp = new XMLHttpRequest(); 
         xmlhttp.open("GET", href, false); 
         xmlhttp.send(); 
         return xmlhttp.responseText; 
        } 

     function callHTML(variable) 
     { 
     alert('inside callHTML'); 
     switch(variable) { 
      case 1: 
       loadPage('file1.html'); 
      break; 
     case 2: 
      alert('case 2'); 
       loadPage('file2.html'); 
      break; 
      case 3: 
       loadPage('file2.html'); 
      break; 
     } 
     } 

     function test() 
     { 
      alert('test'); 
     } 
     document.getElementById("htmlinvoke").innerHTML = callHTML(2); 

     </script> 
    </body> 
</html> 

UPDATE 기존 HTML 페이지에서 HTML 페이지를 호출 할 수있는 업데이트 된 코드입니다 ..로드하지 않습니다.

편집

당신이 만일), 비동기 호출에 대한주의, BTW) =

<html> 
    <body> 
    <p>This example calls different HTML pages based on the input</p> 
     <p id="htmlinvoke"></p> 
     <script> 
     function loadPage(href) 
        { 
        window.location.href = href; 

        } 

     function callHTML(variable) 
     { 
     //alert('inside callHTML'); 
     switch(variable) { 
      case 1: 
       loadPage('file1.html'); 
      break; 
     case 2: 
      //alert('case 2'); 
       loadPage('file2.html'); 
      break; 
      case 3: 
       loadPage('file2.html'); 
      break; 
     } 
     } 

     function test() 
     { 
      alert('test'); 
     } 
     document.getElementById("htmlinvoke").innerHTML = callHTML(1); 

     </script> 
    </body> 
</html> 
+1

질문 질문 자체. 분명한 문제 설명이없는 질문은 다른 독자에게 유용하지 않습니다. See : 최소한의 완전하고 검증 가능한 예제를 만드는 방법. –

+0

은 ASYNK 통화이기 때문에. 많이 질문이 있습니다. – cosset

답변

1

스위치가 닫는 } 누락은, 어쩌면 링크가

을 확인하시기 바랍니다 다른 페이지로 이동하고 싶지 않은 이유는 무엇입니까?

window.location.href = "http://example.com/new_url"; 

매우 간단/유용;)

+0

코드를 업데이트했습니다. – RDX

1

"callHTML"기능에서 "스위치"에 "}"이 없습니다. 좀 더 쉽게 코드를 작성하여 쉽게 더 쉽게 현지화 할 수 있습니다.

편집 : 그것을 재현 할 필요가 원하는 동작, 특정 문제 또는 오류와 짧은 코드를 포함해야 디버깅 도움 (? "왜이 코드가 작동하지 않습니다")를 추구



    function callHTML(variable) { 
     alert('inside callHTML'); 

     switch(variable) { 
      case 1: 
       loadPage('file1.html'); 
       break; 
      case 2: 
       alert('case 2'); 
       loadPage('file2.html'); 
       break; 
      case 3: 
       loadPage('file2.html'); 
       break; 
     } 
    } 
+0

감사합니다. 코드를 업데이트했습니다. – RDX