HTML <스크립트> 태그
예시
"안녕 자바스크립트!"라고 쓰세요. 자바스크립트로:
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
정의 및 사용
<script>
태그는 클라이언트 측 스크립트(JavaScript)를 포함하는 데 사용됩니다 .
요소 는 <script>
스크립팅 문을 포함하거나 src 속성을 통해 외부 스크립트 파일을 가리킵니다.
JavaScript의 일반적인 용도는 이미지 조작, 양식 유효성 검사 및 콘텐츠의 동적 변경입니다.
팁 및 참고 사항
팁: 또한 브라우저에서 스크립트를 비활성화했거나 클라이언트 측 스크립팅을 지원하지 않는 브라우저를 사용하는 사용자 의 경우 <noscript> 요소를 살펴보십시오 .
팁: JavaScript에 대해 자세히 알아보려면 JavaScript 자습서 를 방문하십시오 .
브라우저 지원
Element | |||||
---|---|---|---|---|---|
<script> | Yes | Yes | Yes | Yes | Yes |
속성
Attribute | Value | Description |
---|---|---|
async | async | Specifies that the script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes) (only for external scripts) |
crossorigin | anonymous use-credentials |
Sets the mode of the request to an HTTP CORS Request |
defer | defer | Specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing (only for external scripts) |
integrity | filehash | Allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated |
nomodule | True False |
Specifies that the script should not be executed in browsers supporting ES2015 modules |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin same-origin strict-origin strict-origin-when-cross-origin unsafe-url |
Specifies which referrer information to send when fetching a script |
src | URL | Specifies the URL of an external script file |
type | scripttype | Specifies the media type of the script |
HTML과 XHTML의 차이점
XHTML에서 스크립트 내부의 콘텐츠는 #PCDATA(CDATA 대신)로 선언됩니다. 즉, 엔터티가 구문 분석됩니다.
이것은 XHTML에서 모든 특수 문자가 인코딩되거나 모든 콘텐츠가 CDATA 섹션 안에 래핑되어야 함을 의미합니다.
<script type="text/javascript">
//<![CDATA[
var i = 10;
if (i < 5) {
// some code
}
//]]>
</script>
전역 속성
<script>
태그는 HTML의 전역 속성도 지원 합니다 .
관련 페이지
HTML 튜토리얼: HTML 스크립트
HTML DOM 참조: 스크립트 개체
JavaScript 튜토리얼: JavaScript 배우기
기본 CSS 설정
대부분의 브라우저는 <script>
다음 기본값으로 요소를 표시합니다.
script {
display: none;
}