Front 공부/Javascript

1장-2. HTML과 JS (script 태그)

erase-jeong 2023. 10. 16. 19:22

HTML과 Javascript

Javascript는 HTML 위에서 동작하는 언어입니다. 그렇다면 어떻게 서로 다른 두 언어를 하나로 합칠 수 있는 것일까요?

이 때 바로 script 태그가 필요함

 

(앞선 게시물에서는 onclick="" 을 통해서 js 구현)

 

 

 

Javascript 는 html 태그를 통해 구현

hello world 는 js 를 통해 구현

 

Q. 둘 차이의 차이점은 뭘까?

 

아래의 코드와 웹 화면을 보자

 

 Javascript로 쓴 코드는 동적이라는 것이다.

1+1을 출력한다고 해 봅시다.   이 때는 HTML과 Javascript로 쓴 코드의 결과가 아래와 같이 달라진다.

즉, HTML로 쓴 코드는 정적이기 때문에 문자 그대로를 출력하지만, Javascript 코드는 동적으로 이를 계산할 수도 있다는 것이다. 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Javascript</h1>
    <script>
        document.write(1+1);
    </script>
    <h1>html</h1>
    1+1
</body>
</html>