WEB/JavaScript
[JS] 외부 스크립트 파일
애플쩀
2022. 5. 27. 16:17
<script src="myScript.js"></script>
이러한 방식으로 추가를 한다.
전체코드
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="myScript.js"></script>
<script>
document.write("홍길동1<br>");
</script>
</head>
<h3>js/ex01.html</h3>
<body>
<h2 onclick="myAlert('테스트')">h2</h2>
<pre>
js 스크립트안에 태그
html 원하는 수 만큼 script 태그를 사용해도 된다.
<script>
document.write("홍길동1<br>");
</script>
</pre>
<!-- 외부 스크립트 파일(myScript.js) -->
디스플레이 속도 늦추지 않게 body 닫기태그 위에한다.
<script>
document.write("홍길동1<br>");
</script>
</body>
</html>
myScript.js
document.write("외부 스크립트 파일입니다. ");
function myAlert(message){
window.alert(message);
}
/*
외부스크립트 장점
html코드와 분리
유지보수확장이 용이
캐싱외부 스크립트 파일은 페이지 로드 속도를 높일 수 있다. */