[javascript] How do I redirect to another webpage?


WARNING: This answer has merely been provided as a possible solution; it is obviously not the best solution, as it requires jQuery. Instead, prefer the pure JavaScript solution.

$(location).attr('href', 'http://stackoverflow.com')

How can I redirect the user from one page to another using jQuery or pure JavaScript?

Here is a time-delay redirection. You can set the delay time to whatever you want:

<!doctype html>
<html lang="en">

    <meta charset="UTF-8">
    <title>Your Document Title</title>
    <script type="text/javascript">
        function delayer(delay) {
            onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);

    <div>You will be redirected in 8 seconds!</div>


# HTML Page Redirect Using jQuery/JavaScript

Try this example code:

function YourJavaScriptFunction()
    var i = $('#login').val();
    if (i == 'login')
        window.location = "login.php";
        window.location = "Logout.php";

If you want to give a complete URL as window.location = "www.google.co.in";.


// window.location
window.location.href = 'http://www.example.com'
document.location.href = '/path'

// window.history

// window.navigate; ONLY for old versions of Internet Explorer

// Probably no bueno
self.location = 'http://www.example.com';
top.location = 'http://www.example.com';

// jQuery
$(location).prop('href', 'http://www.example.com')

This works with jQuery:

$(window).attr("location", "http://google.fr");

var url = 'asdf.html';
window.location.href = url;

But if someone wants to redirect back to home page then he may use the following snippet.

window.location = window.location.host

It would be helpful if you have three different environments as development, staging, and production.

You can explore this window or window.location object by just putting these words in Chrome Console or Firebug's Console.

jQuery is not needed. You can do this:


It is that easy!

The best way to initiate an HTTP request is with document.loacation.href.replace('URL').


To just redirect to a page with JavaScript:

  window.location.href = "/contact/";

Or if you need a delay:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds

jQuery allows you to select elements from a web page with ease. You can find anything you want on a page and then use jQuery to add special effects, react to user actions, or show and hide content inside or outside the element you have selected. All these tasks start with knowing how to select an element or an event.

             opacity: 0 //Put some CSS animation here
         }, 500);
           // OK, finished jQuery staff, let's go redirect
           window.location.href = "/contact/";

Imagine someone wrote a script/plugin that is 10000 lines of code?! Well, with jQuery you can connect to this code with just a line or two.

It would help if you were a little more descriptive in what you are trying to do. If you are trying to generate paged data, there are some options in how you do this. You can generate separate links for each page that you want to be able to get directly to.

<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>

Note that the current page in the example is handled differently in the code and with CSS.

If you want the paged data to be changed via AJAX, this is where jQuery would come in. What you would do is add a click handler to each of the anchor tags corresponding to a different page. This click handler would invoke some jQuery code that goes and fetches the next page via AJAX and updates the table with the new data. The example below assumes that you have a web service that returns the new page data.

$(document).ready( function() {
    $('a.pager-link').click( function() {
        var page = $(this).attr('href').split(/\?/)[1];
            type: 'POST',
            url: '/path-to-service',
            data: page,
            success: function(content) {
               $('#myTable').html(content);  // replace
        return false; // to stop link

In JavaScript and jQuery we can use the following code to redirect the one page to another page:


On your click function, just add:

window.location.href = "The URL where you want to redirect";
    window.location.href = "http://www.google.com";

First write properly. You want to navigate within an application for another link from your application for another link. Here is the code:

window.location.href = "http://www.google.com";

And if you want to navigate pages within your application then I also have code, if you want.

I just had to update this ridiculousness with yet another newer jQuery method:

var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);