How to define a client-side script in HTML5 ?
To define a client-side script in HTML5, we can do <script> tag or adding external script file by src attribute to the HTML file. <script> tag contains the JavaScript to be added for the client-side scripting.
Syntax:
<script> // JavaScript goes here console.log('w3wiki'); </script>
Below are the examples for the above approach:
Example 1: Here script is added using <script> tag.
HTML
<!DOCTYPE html> < html > < body > < h1 >Welcome To GFG</ h1 > < p id = "id1" > Default code has been loaded into the Editor. </ p > < script > document.getElementById("id1").innerHTML= "Welcome to w3wiki" </ script > </ body > </ html > |
Output:
Example 2: Here, external JavaScript is used for scripting.
index.html
<!DOCTYPE html> < html > < body > < h2 >External JavaScript</ h2 > < p id = "id1" > This is before you click the button </ p > < button type = "button" onclick = "clickMe()" > Click Me </ button > < script src = "main.js" ></ script > </ body > </ html > |
main.js
function clickMe() { document.getElementById( "id1" ).innerHTML = "This is after you click the button" ; } |
Output: