자바스크립트 선언문
- 자바스크립트 구문을 실행하기 위해서는 선언문을 입력해야 합니다.
- <head> 영역이나 <body> 영역에 선언하면 됩니다.
- <head> 영역
1 2 3 4 5 6 7 8 9 10 | < html > < head > < script type = "text/javascript" > javascript statements; </ script > </ head > < body > </ body > </ html > |
- <body> 영역
1 2 3 4 5 6 7 8 9 | < html > < head > </ head > < body > < script type = "text/javascript" > javascritp statements; </ script > </ body > </ html > |
자바스크립트 위치
- 자바스크립트 코드가 html 상에서 올 수 있는 위치는 3곳입니다.
- Inner
- Outside
- Inline
- Inner
- 자바스크립트 코드가 html 문서 안에 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | < html > < head > < title >My First Javascript </ title > < script > document.write("Hello World!"); </ script > </ head > < body > </ body > </ html > |
- Outside
- 자바스크립트 파일을 외부에서 html 문서로 가져오는 형태 입니다. 자바스크립트 파일은 'js' 확장자를 사용합니다.
1 2 3 4 5 6 7 | < html > < head > < script src = "helloworld.js" ></ script > </ head > < body > </ body > </ html > |
- Inline
- html에서 사용하는 태그 안에서 onclick="alert('helloworld!!.')" 와 같이 자바스크립트 구문이 사용되는 형태입니다.
1 2 3 4 5 | < html > < body > < button type = "button" onclick="alert(‘helloworld!!.’)”>click button!</ button > </ body > </ html > |