Skip to content
Related Articles

Related Articles

Improve Article

How to define a client-side script in HTML5 ?

  • Last Updated : 22 Mar, 2021

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('GeeksforGeeks');
</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 GeeksForGeeks"
    </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:

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :