외부 파일로 분리(Separate into external file)

2018. 5. 5. 11:19클라이언트/JavaScript

외부 파일로 분리 (Separate into external file)

 

JS를 별도의 파일로 분리할 수도 있다. 장점을 보다 엄격하게 정보와 제어를 분리할 수 있다는 점이다.

 

하나의 JS파일을 여러 웹 페이지에서 로드함으로 JS의 재활용률을 높일 수 있다.

 

캐쉬를 통해 속도의 향상, 전송량의 경량화를 도모할 수 있다.

 

html 파일

예제코드

 

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
    <body>
        <input type="button" id="hw" value="Hello World" />
        <script src="./script.js"></script>
    </body>
</html>
 
 
cs

 

 

분리된 script.js 파일

예제코드

 

1
2
var hw = document.getElementById('hw');
hw.addEventListener('click',function(){alert('hello World')});
cs

 

'클라이언트 > JavaScript' 카테고리의 다른 글

Inline 방식과 Script 방식  (0) 2018.05.05