HTML and JavaScript are two different languages that work together to create dynamic and interactive web pages. HTML (Hypertext Markup Language) is used to create the structure and content of web pages, while JavaScript is used to add interactivity and dynamic behavior to those pages.
JavaScript is a programming language that can be embedded directly into HTML documents using the <script>
tag. Here’s an example of how to include JavaScript code in an HTML document:
<!DOCTYPE html> <html> <head> <title>My Web Page</title> </head> <body> <h1>Welcome to my web page!</h1> <p>Click the button below to change the text.</p> <button onclick="changeText()">Click me</button> <script> function changeText() { var text = document.querySelector('p'); text.textContent = 'The text has been changed!'; } </script> </body> </html>
In this example, we’ve included a JavaScript function called changeText()
inside the <script>
tag. When the user clicks the button on the page, the changeText()
function is called, which finds the first paragraph on the page and changes its text content to “The text has been changed!”.
JavaScript can also be stored in external files and linked to an HTML document using the <script>
tag. Here’s an example of how to link an external JavaScript file:
<!DOCTYPE html> <html> <head> <title>My Web Page</title> <script src="myscript.js"></script> </head> <body> <h1>Welcome to my web page!</h1> <p>The text on this page will change automatically.</p> </body> </html>
In this example, we’ve linked an external JavaScript file called myscript.js
to our HTML document using the <script>
tag in the <head>
section of the document. The code in myscript.js
could contain functions that update the text on the page, create animations, or interact with APIs.
In summary, HTML and JavaScript are two separate languages that work together to create dynamic and interactive web pages. JavaScript can be embedded directly into HTML documents or stored in external files and linked to the HTML document using the <script>
tag.