2012-02-24 2 views
2

다음 코드 스 니펫을 사용하여 비대칭 방식으로 비동기 적으로 JavaScript를로드합니다. Chrome, FF에서는 작동하지만 Internet Explorer에서는 작동하지 않습니다.Internet Explorer에서 익명 함수가 작동하지 않습니다.

저는 IE8을 실행 중이며 아래 코드에 대해 IE에서 onload 기능을 사용할 수 없습니다.

  <script type="text/javascript"> 
      (function() { 
       var s = document.createElement('script'); 
       s.type = 'text/javascript'; 
       s.async = true; 
       s.src = 'js/load_outer.js'; 
       s.onload = function() { 
        alert("Loaded"); 
       } 

       var x = document.getElementsByTagName('script')[0]; 
       x.parentNode.insertBefore(s, x); 
      })(); 
     </script> 

누구든지 실수를 식별 할 수 있도록 도와 주시겠습니까?

감사

+0

당신은 나 "로드"이벤트를하지 않을 수 있습니다 때 스크립트로드 IE에서. – Pointy

+0

다음은 관련 질문입니다. [http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer](http://stackoverflow.com/questions/4845762/onload- script-handler-for-internet-explorer 태그). – jabclab

+0

@jakeclarkson 링크를 제공해 주셔서 감사합니다. 그러나 타사 라이브러리를 사용하지 말아야했습니다. – Jayesh

답변

2

IE (9 이전) 대신 onreadystatechange를 사용 <script> 요소에 대한 onload 이벤트를 지원하지 않습니다

var complete = false; 
script.onload = script.onreadystatechange = function() { 
    if (!complete && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) { 
     complete = true; 
     // your callback code here 
    } 
} 
+1

도움이되었습니다. 나는 IE9와 비슷한 문제로 다시 붙어있다. 이중 콜백을 제공합니다. 다음은이를 해결하는 기사입니다 - http://www.aaronpeters.nl/blog/prevent-double-callback-execution-in-IE9 – Jayesh

관련 문제