Bhubaneswar, Odisha, India
+91 8637 274 400

Navigating to Another Page by Clicking a Button Using Basic JavaScript

Navigating to Another Page by Clicking a Button Using Basic JavaScript

Navigating to a different page when a button is clicked is a common task in web development. Using JavaScript, this can be accomplished easily and effectively. This blog post will guide you through the process step-by-step, ensuring you understand the fundamentals of JavaScript and its interaction with HTML elements.

Are you looking for Live Training in C#?

The upcoming batch for C#.NET is sceduled.

Book Your Seat Now.

Step 1: Setting Up the HTML Structure

First, create a simple HTML file with a button element. This button will be used to trigger the navigation to another page.

HTML





<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Navigate to Another Page</title>
</head>
<body>
    <button id="navigateButton">Go to Page</button>

    <script src="script.js"></script>
</body>
</html>

In this HTML structure:

  • We have a button element with the ID navigateButton.
  • We include an external JavaScript file named script.js where our navigation logic will be implemented.

Step 2: Writing the JavaScript Code

Next, we need to write the JavaScript code to handle the button click event and navigate to another page.

JavaScript (script.js)

Create a new file named script.js and add the following code:

document.addEventListener('DOMContentLoaded', function() {
    var button = document.getElementById('navigateButton');

    button.addEventListener('click', function() {
        window.location.href = 'https://www.example.com';
    });
});

Are you looking for Live Training in C#?

The upcoming batch for C#.NET is sceduled.

Book Your Seat Now.

In this JavaScript code:

  • We wait for the DOM to fully load using DOMContentLoaded.
  • We select the button element using document.getElementById.
  • We add a click event listener to the button that changes the window.location.href property to the URL of the page we want to navigate to.

Explanation

  • document.addEventListener('DOMContentLoaded', function() {...});: This ensures that the JavaScript code runs only after the HTML document has been fully loaded and parsed.
  • var button = document.getElementById('navigateButton');: This retrieves the button element from the DOM.
  • button.addEventListener('click', function() {...});: This adds a click event listener to the button.
  • window.location.href = 'https://www.example.com';: This line sets the href property of window.location to the desired URL, causing the browser to navigate to that URL.

Step 3: Testing the Navigation

To test the navigation functionality, follow these steps:

  1. Save the index.html and script.js files.
  2. Open index.html in a web browser.
  3. Click the “Go to Page” button.

You should see the browser navigate to the specified URL (https://www.example.com in this example).

Customizing the Navigation

You can customize the navigation URL based on your needs. For instance, you might want to navigate to a different page within your own website. Simply change the URL in the JavaScript code:

button.addEventListener('click', function() {
    window.location.href = 'about.html';
});

This will navigate to about.html in the same directory as your current page.

Conclusion

Navigating to another page by clicking a button using basic JavaScript is a fundamental task in web development. By following the steps outlined in this blog post, you can easily implement this functionality in your own projects. Understanding how to interact with the DOM and handle events is crucial for creating dynamic and interactive web applications.

You can refer the video link to understand the concept better:

Are you looking for Live Training in C#?

The upcoming batch for C#.NET is sceduled.

Book Your Seat Now.