HTML JavaScript

JavaScript is a programming language that is used to add dynamic functionality and interactivity to web pages. JavaScript code can be embedded directly into HTML web pages using the <script> element, or included as separate files.

Here's an example of how to add a basic JavaScript script to an HTML web page:

<script>
  alert("Hello, world!");
</script>

In this example, we use the <script> element to embed a JavaScript script in the web page. The script consists of a single statement that displays an alert dialog box with the message "Hello, world!".

Here are some other examples of how to use JavaScript in HTML:

  • Using JavaScript to modify HTML:
<p id="demo">This is some text.</p>
<script>
  document.getElementById("demo").innerHTML = "This is new text.";
</script>

In this example, we use JavaScript to modify the contents of an HTML element with the ID "demo". The script uses the document.getElementById() method to find the element, and the .innerHTML property to change its contents.

  • Adding event handlers with JavaScript:
<button onclick="alert('Button clicked!')">Click me</button>

In this example, we use the onclick attribute to add a JavaScript event handler to a button. When the button is clicked, an alert dialog box with the message "Button clicked!" will be displayed.

JavaScript can be customized using CSS to change its appearance and behavior, and can be combined with other web technologies like AJAX to create more advanced web applications. By using JavaScript effectively, you can create more dynamic and interactive web pages.