오보에블로그

[자바스크립트] 기본 세팅 및 문법 본문

STEADYSTUDY/넓고 얕게

[자바스크립트] 기본 세팅 및 문법

(OBO) 2020. 7. 11. 18:35
728x90

html 기본 틀 작성 (파일 명 :  hellow.js , hellow.html)

<!DOCTYPE html>
<html lang=ko>
    <head>
        <meta charset = "utf-8">
        <!-- html 글자 깨짐 방지 -->
        <title> 자바스크립트</title>
        <script src="hellow.js" defer> </script>
        <!-- 자바스크립트 파일 불러오기 -->
        <!-- <script>
        var hello = "hi every one!";
        document.body.innerText = hello;
        </script> -->
        <!-- html 코드에서 자바스크립트 싫행시키기 ; 필수 -->
    </head>
    <body>

    </body>
</html>

 

defer : 서버에서 hello.js 파일을 가져오고 바로 코드를 실행한다.

코드에서 바디를 렌더링 하지 않았으므로,,, 에러..!

defer 써놓으면 바디까지 렌더링 후에 js 파일 코드 실행

 

자바스크립트 코드 작성

var jbRandom = Math.random();
var jbRandom = Math.floor(jbRandom * 10 ); 
var hello = "안녕하세요";
var nothello = "아니..안녕하세요";
if(jbRandom < 5)
{
    document.body.innerText = hello;
};
if(jbRandom >= 5)
{
    document.body.innerText = nothello;
};

0-9까지의 랜덤 변수를 받고, 만약에 랜덤 변수가 5보다 작으면 hello 변수 출력 , 아니면 nothello 변수 출력

hello.html을 부를때마다 써지는 텍스트가 달라지는것을 알 수 있다.

728x90