
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to use the
In this tutorial, we will learn how to use the <script> tag to define client-side JavaScript.
The HTML <script> tag is used for declaring a script within your HTML document. Through this, you can define client-side JavaScript. Users can write the client-side JavaScript code directly in the script tag, or the script tag can be pointed to an external JavaScript file using the ‘src’ attribute.
The client-side JavaScript can be written in multiple parts of the HTML file, such as inside the body tag, head tag, or inline of an element tag. Still, writing any JavaScript code in between the script tag inside the body tag only is recommended.
Syntax
<html> <head> </head> <body> ... <!-- Script tag goes here --> <script> // JavaScript code </script> </body> </html>
The above syntax shows the recommended place to use the script tag to write JavaScript code.
Attributes
Attribute | Value | Description |
---|---|---|
async | async | It specifies that the script is executed asynchronously. |
defer | defer | It specifies that the script will generate no content. As a result, the browser/user agent can proceed with parsing and rendering the rest of the page. |
charset | charset | It specifies the character encoding used by the script. |
src | URL | It specifies the URI/URL of an external script. |
type | application/ecmascript text/JavaScript text/vbscript application/JavaScript | It specifies the scripting language as a content type or MIME type. |
crossorigin | anonymous use-credentials | It specifies the mode of the request to an HTTP CORS Request. |
integrity | filehash | It allows a browser to validate the fetched script, ensuring that the code is never loaded if the source has been manipulated. |
Example 1
In the below example, we have used the script tag to define client-side JavaScript. We have used the script tag inside of the body tag. The JavaScript code here will change a text of an element with a button click. The button “Change text” is associated with a click event that executes the “changeText()” function. This function changed the element’s content using the document.getElementById() method.
<html> <body> <h2>Using the <i>script tag</i> to to define client-side JavaScript</h2> <button onclick = "changeText()"> Change text </button> <div id = "root" style = "padding: 10px; background-color: bisque; border: 1px solid black"> This element's text will change after the button click! </div> <!-- script tag --> <script> function changeText() { const root = document.getElementById('root') root.innerHTML = 'Welcome to Tutorialspoint!' } </script> </body> </html>
Example 2
In the below example, we have used the script tag to define client-side JavaScript to update the style of an element. We have placed the script tag inside at the end of the body tag. The script tag contains the JavaScript code to change the background color of an element using a button click. Two buttons, ‘Blue’ and ‘Green’, are associated with click events that execute their respective functions to change the background color to blue and green, respectively. The document.getElementById() method is used to change the background color.
<html> <body> <h2>Using the <i>script tag</i> to define client-side JavaScript</h2> <button onclick = "bgBlue()" >Blue</button> <button onclick = "bgGreen()" >Green</button> <div id = "root" style = "padding: 10px; margin: 5px 0px; background-color: blue; border: 1px solid black; color: white;"> This element's background color will change by button click! </div> <!-- script tag --> <script> // element const root = document.getElementById('root') function bgBlue() { root.style.backgroundColor = 'blue' } function bgGreen() { root.style.backgroundColor = 'green' } </script> </body> </html>
This tutorial taught us to use the <script> tag to define client-side JavaScript. We have discussed the syntax and attributes of the script tag. In the first example, we use the script tag and write the JavaScript code in between it to change an element’s content. In the second example, we use the script tag to change the background color of an element. Users can write any JavaScript code in between the script tag. It is recommended to go through the examples to understand the topic better.
- Related Articles
- How to use JavaScript to create client-side image map?
- How to use the tag to define HTML document title?
- How to use the tag to define a relationship to an external resource?
- How to use the tag to define style information for an HTML page?
- How to use the tag to define the base URL for an HTML page?
- How to use the tag in HTML?
- How to use the tag in HTML?
- How to use the tag in HTML?
- How to display HTML5 client-side validation error bubbles?
- Client side validation with HTML and without JavaScript
- How to define media type of style tag in HTML5?
- How to use title tag in HTML Page?
- How to Use the mysql Client and Related Utilities in Linux
- How to use Meta Tag to redirect an HTML page?
- How do we use function literal to define a function in JavaScript?
