HTML script Tag
The <script> tag in HTML is used to define the client-side script. The <script> tag contains the scripting statements, or it points to an external script file. The JavaScript is mainly used in form validation, dynamic changes of content, image manipulation, etc.
Syntax:
<script> Script Contents... </script>
Attributes: Many attribute associated with script tag.
- async: It is used to specify the script is executed asynchronously.
- charset: It is used to specify the character encoding used in an external script file.
- defer: It is used to specify that the script is executed when the page has finished parsing.
- src: It is used to specify the URL of an external script file.
- type: It is used to specify the media type of the script.
Example 1:
HTML
<!DOCTYPE html> < html > < body > < h1 >GeeksforGeeks</ h1 > < h2 >< script > Tag</ h2 > < p id = "Geeks" ></ p > <!-- html script tag starts here --> < script > document.getElementById("Geeks").innerHTML = "Hello GeeksforGeeks!"; </ script > <!-- html script tag ends here --> </ body > </ html > |
Output:
Example 2(script outside body tag):
HTML
<!DOCTYPE html> < html > < head > < title >script tag</ title > < style > body { text-align:center; } h1 { color:green; } </ style > < script > function Geeks() { alert('Welcome to GeeksforGeeks!'); } </ script > </ head > < body > < h1 >GeeksforGeeks</ h1 > < h2 >< script > Tag</ h2 > < button type = "button" onclick = "Geeks()" > Hello GeeksforGeeks</ button > </ body > </ html > |
Output:
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari