티스토리 뷰
자바스크립트에서 js import
개요
- How to include a javascript file in another javascript file
- JavaScript에서 js import
- 자바스크립트에서 자바스크립트 import
- 방법 1, 2는 callback 함수 내부에서만 제대로 이용할 수 있다.
- 별도의 문서로 불러오는 방식
- 방법 3은 callback 함수 필요없이, HTML에서 import하는 것과 동일하게 이용할 수 있다.
- 문서 자신의 헤드에 붙이는 방식
방법 1: JavaScript
- import.php
<script> function loadScript(url, callback) { var script = document.createElement('script'); script.src = url; script.onload = callback; document.getElementsByTagName('head')[0].appendChild(script); } var myloaded = function() { document.write(str); // hello 출력됨 } loadScript('import_hello.js', myloaded); </script>
- import_a.js
var str = 'hello';
방법 2: jQuery
- import2.php
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script> $.getScript("import_hello.js", function() { document.write(str); // hello 출력됨 }); </script>
방법 3: jQuery 2
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script> $('head').append('<script src=\'import_hello.js\'><\/script>'); document.write(str); // hello 출력됨 </script>
Powered by Disqus
'프로그래밍,PLC,계장' 카테고리의 다른 글
[eclipse] eclipse 서버설정문제(next버튼이 비활성화 되었을때) / eclipse server setting problem (next button diable) (0) | 2016.04.12 |
---|---|
get방식으로 받아온 URL에서 스크립트로 변수 가져오기 (0) | 2015.05.14 |
[JSP] Get 방식과 Post 방식의 처리 (0) | 2015.05.06 |
JSP내에서 메소드(함수)를 정의해 사용하는 방법 (0) | 2015.05.06 |
톰캣(Tomcat) 구동시 Multiple Contexts have a path of 에러 (0) | 2015.05.06 |
댓글