2013-06-12 4 views
0

다음 스크립트를 사용하여 XML 문서를 구문 분석하고 웹 사이트에 표시하면이 모든 것이 효과적이지만 HTML 문서 중간에이 스크립트가 있습니다.XML 구문 분석 블로킹 페이지로드

약 300ms 내 페이지 로딩을 막아 버린 것 같습니다. 그래서이 문제를 해결하기 위해 문서 끝에 넣고 싶습니다.

내 질문은 ... 문서의 끝에이 스크립트를 어떻게 만들 수 있습니까? 문서의 중간에 출력을 표시하는 방법은 무엇입니까?

<script> 
if (window.XMLHttpRequest) { 
    xhttp = new XMLHttpRequest(); 
} else { 
    xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xhttp.open("GET", "abc.xml", false); 
xhttp.send(); 
xmlDoc = xhttp.responseXML; 
x = xmlDoc.getElementsByTagName("xr")[0].attributes; 
att = x.getNamedItem("value"); 
document.write(att.value); 
</script> 

감사!

답변

0

당신이 온 클릭 기능을 괜찮다면 ...

<body> 
    <div onclick="showXML()">Click here to show XML</div> 
    </body> 
    <script> 
    function showXML(){ 
    if (window.XMLHttpRequest) { 
     xhttp = new XMLHttpRequest(); 
    } else { 
     xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xhttp.open("GET", "abc.xml", false); 
    xhttp.send(); 
    xmlDoc = xhttp.responseXML; 
    x = xmlDoc.getElementsByTagName("xr")[0].attributes; 
    att = x.getNamedItem("value"); 
    document.write(att.value); 
    } 
    </script> 
+0

이 필요 로드 및 onclick보다는 오히려 나는 두려워, 나는 $ (document) .ready() 덕분에 사이트에 jquery를 사용하고 있습니다. – itezora

+0

잠깐, 그래서 그것을 해결 했습니까? onload와 $ (document) .ready() @ user2201059? – AlvinArulselvan

+0

아니, 나는 그것을 해결하지 못했다. 제발 그 방법 (또는 다른 onload 메서드)으로 그것을하는 방법을 보여줄 수 있다면 제발, 위대한 것입니다! :) – itezora

0

당신이 jQuery 라이브러리를 사용하는 경우, 당신이 시도 할 수있는 다음과 같은 것은 (putting the script at the bottom) :

<body> 
    <div id="test">Click here to show XML</div> 
    <script> 
     $(document).ready(function(){ 
      function showXML(){ 
       if (window.XMLHttpRequest) { 
       xhttp = new XMLHttpRequest(); 
       } else { 
       xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       xhttp.open("GET", "abc.xml", false); 
       xhttp.send(); 
       xmlDoc = xhttp.responseXML; 
       x = xmlDoc.getElementsByTagName("xr")[0].attributes; 
       att = x.getNamedItem("value"); 
       document.write(att.value); 
      } 
      $("#test").on('click', function(){ 
       showXML(); 
      }); 
     }); 
    </script> 
</body> 
+0

어떻게하면 클릭없이이 방법을 사용할 수 있습니까? 이상 적으로로드하고 자동으로 표시하고 싶지만 페이지로드를 차단하지 않을 것입니다 .. 감사합니다. – itezora