HTML <script> tagS2C Home   « HTML <script> tag

Definition

The <script> tag is a self closing tag for defining an inline script or imported script to be embedded within the HTML document.

Example


<!DOCTYPE html>   <!-- The DOCTYPE declaration always comes first in a HTML document --> 
<html  lang="en">   <!-- Always enter a language for a webpage -->
<head>
  <title>Appears in browser toolbar, favourites and search engine results.</title>
</head>

<body>
  
  <script>
    function changeExampleBg(color){
      document.getElementById("example").style.background = color;
    }
  </script>
  
  <p id="example">Some random text:</p>
  <label>Change example background colour to:</label>
  <button type="button" onclick="changeExampleBg('cyan')">Cyan</button>
  <button type="button" onclick="changeExampleBg('orange')">Orange</button>

  <!-- Put imported scripts just before closing body tag for efficiency -->
  <script src="../js/jquery.js"></script>
</body>
</html>

Some random text.

Attributes

The following attributes can be used with the <script> tag.


<script> Specific Attributes
Attribute Value Description Example
asyncValid values an empty string or asyncSpecifies whether the script is executed asynchronously.

Can only be used when the src attribute is also present.

<script async 
        src="../../js/jquery.js">Script content</script>
<script async="async" 
        src="../../js/jquery.js">Script content</script>;
charsetcharsetSpecifies the character encoding of this script.

<script type="text/javascript" 
        charset="ISO-8859-1">Script content</script>
crossorigin

anonymous


use-credentials
Specifies how the resource handles cross-origin request.

Requests for the element will have their mode set to "cors" and their credentials mode set to "same-origin".

Requests for the element will have their mode set to "cors" and their credentials mode set to "include".

<script crossorigin="use-credentials" 
        src="../js/jquery.js">Script content</script>
deferValid values an empty string or deferSpecifies whether execution of the script is delayed until the page has loaded.

<script defer 
        src="../../js/jquery.js">Script content</script>
<script defer="defer" 
        src="../../js/jquery.js">Script content</script>
srcurlSpecifies the URL location of an external script.

<script src="../../js/jquery.js">Script content</script>
typemedia_typeSpecifies the MIME type of a script.

Default value is text/javascript so this can be omitted for JavaScript files.

<script type="text/javascript" 
        src="../js/jquery.js">Script content</script>
<script> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<script class="name">Script content</script>
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<script id="name">Script content</script>
styleSpecifies an inline style for the element allowing you to apply the style to the contentt.

<script style="color:red;">Script content</script>
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<script title="Content info">Script content</script>
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<script accesskey="a">Script content</script>
tabindexSpecifies a tab order for the element.

<script tabindex="1">Script content</script>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<script dir="ltr">Script content</script>
<!-- The text will flow from right to left -->
<script dir="rtl">Script content</script>
langSpecifies a language code for the content of the element.

<script lang="en">Script content</script>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the contentt.

<!-- Valid values true and false. -->
<!-- Default inherited / browser specific. -->
<script spellcheck="true">Script content</script> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<script translate="no">Script content</script> 
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<script contenteditable="true">Script content</script> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<script draggable="true">Script content</script>
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<script hidden>Script content</script> 
<script hidden="hidden">Script content</script> 
<script> Event Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<script oncopy="go()">Script content</script> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<script oncut="go()">Script content</script>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<script onpaste="go()">Script content</script>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<script ondrag="go()">Script content</script>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<script ondragend="go()">Script content</script>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<script ondragenter="go()">Script content</script>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<script ondragleave="go()">Script content</script>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<script ondragover="go()">Script content</script>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<script ondragstart="go()">Script content</script>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<script ondrop="go()">Script content</script>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<script onblur="go()">Script content</script>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<script onchange="go()">Script content</script>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<script oncontextmenu="go()">Script content</script>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<script onfocus="go()">Script content</script>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<script oninput="go()">Script content</script>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<script oninvalid="go()">Script content</script>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<script onreset="go()">Script content</script>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<script onselect="go()">Script content</script>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<script onsubmit="go()">Script content</script>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<script onkeydown="go()">Script content</script>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<script onkeypress="go()">Script content</script>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<script onkeyup="go()">Script content</script>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<script onabort="go()">Script content</script>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<script oncanplay="go()">Script content</script>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<script oncanplaythrough="go()">Script content</script>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<script oncuechange="go()">Script content</script>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<script ondurationchange="go()">Script content</script>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<script onemptied="go()">Script content</script>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<script onended="go()">Script content</script>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<script onloadeddata="go()">Script content</script>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<script onloadedmetadata="go()">Script content</script>
onloadstartThe script to be run whenthe media resource has started loading.

 <!-- Executes go() function -->
<script onloadstart="go()">Script content</script>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<script onpause="go()">Script content</script>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<script onplay="go()">Script content</script>
onplayingThe script to be run when when playback has already begun.

 <!-- Executes go() function -->
<script onplaying="go()">Script content</script>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<script onprogress="go()">Script content</script>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<script onratechange="go()">Script content</script>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<script onseeked="go()">Script content</script>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<script onseeking="go()">Script content</script>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<script onstalled="go()">Script content</script>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<script onsuspend="go()">Script content</script>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<script ontimeupdate="go()">Script content</script>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<script onvolumechange="go()">Script content</script>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<script onwaiting="go()">Script content</script>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<script onclick="go()">Script content</script>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<script ondblclick="go()">Script content</script>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<script onmousedown="go()">Script content</script>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<script onmousemove="go()">Script content</script>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<script onmouseout="go()">Script content</script>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<script onmouseover="go()">Script content</script>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<script onmouseup="go()">Script content</script>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<script onwheel="go()">Script content</script>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<script onscroll="go()">Script content</script> 
Window - NONE

Relevant HTML Tutorials

HTML Advanced - Scripting