How to Redirect to Another Webpage Using JavaScript or jQuery


Redirecting a user from one webpage to another is a common requirement in web development. Whether you want to redirect the user after a successful form submission, handle expired sessions, or simply navigate to a different page, JavaScript and jQuery offer several methods to accomplish this task.

Method 1: Using JavaScript's window.location property

The simplest way to redirect to another webpage is by using JavaScript's window.location property. This property determines the current URL of the window and allows us to assign a new URL to navigate to:

window.location = "";

This code snippet will immediately redirect the user to the specified URL. You can place this code in an event listener, such as a button click or form submission, to redirect the user after a specific action.


// Redirect the user to after 3 seconds
setTimeout(() => {
  window.location = "";
}, 3000);

In this example, the user will be redirected to "" after a delay of 3 seconds. You can adjust the delay time to fit your requirements.

Method 2: Using JavaScript's location.href property

Another way to redirect the user is by utilizing the location.href property. This property behaves similarly to window.location and can be used interchangeably:

location.href = "";

This code performs a page redirect to the specified URL. It is important to note that both window.location and location.href triggers a browser navigation, meaning the user can hit the back button to return to the previous page.


// Redirect the user to when clicking a button
document.getElementById("myButton").addEventListener("click", function() {
  location.href = "";

By adding an event listener to a button with the ID "myButton", we can redirect the user to "" when the button is clicked.

Method 3: Using JavaScript's method

If you want to redirect the user to a new page while opening it in a new tab or window, you can use the method:"");

This code will open "" in a new tab or window, depending on the user's browser settings. It is important to note that some browsers may block new window openings, so this method might not always work as expected.


// Open in a new window when clicking a link
document.getElementById("myLink").addEventListener("click", function(e) {

In this example, we prevent the default behavior of the link by using e.preventDefault() and open "" in a new window when the link with the ID "myLink" is clicked.

Method 4: Using jQuery's window.location property

If you are using jQuery in your project, you can also perform a page redirect using the window.location property just like in pure JavaScript:

$(window).attr("location", "");

By setting the location attribute of the window object, you can redirect the user to the specified URL.


// Redirect the user to after an animation completes
$("#myElement").animate({opacity: 0}, 2000, function() {
  $(window).attr("location", "");

In this example, we animate the opacity of an element with the ID "myElement" over a duration of 2 seconds. After the animation completes, the user will be redirected to "".


Redirecting to another webpage is a common task in web development, and JavaScript and jQuery provide several methods to accomplish it. By using JavaScript's window.location or location.href properties, you can perform a page redirect to a specified URL. Additionally, the method allows you to open a new tab or window with the desired URL. If you're using jQuery, you can use $(window).attr("location", ...) to achieve the same result. Choose the method that best fits your requirements and integrate it into your code to redirect users seamlessly.