Home How to redirect to another webpage?
Reply: 30

How to redirect to another webpage?

venkatachalam Published in 2009-02-02 12:54:16Z

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

The Red Pea
The Red Pea Reply to 2017-09-23 18:42:25Z

One does not simply redirect using jQuery

jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

If you want to simulate someone clicking on a link, use location.href

If you want to simulate an HTTP redirect, use location.replace

For example:

// similar behavior as an HTTP redirect

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
tvanfosson Reply to 2009-02-02 13:18:06Z

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
Ben Lee
Ben Lee Reply to 2012-05-01 20:35:48Z
var url = 'asdf.html';
window.location.href = url;
royhowie Reply to 2015-08-03 03:01:20Z

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')
Jeremy W
Jeremy W Reply to 2014-12-04 20:30:50Z

This works for every browser:

window.location.href = 'your_url';
Peter Mortensen
Peter Mortensen Reply to 2016-10-30 16:11:26Z

This works with jQuery:

$(window).attr("location", "http://google.fr");
Peter Mortensen
Peter Mortensen Reply to 2014-11-29 06:13:56Z

You can do that without jQuery as:

window.location = "http://yourdomain.com";

And if you want only jQuery then you can do it like:

Peter Mortensen
Peter Mortensen Reply to 2016-10-30 16:12:55Z

Standard "vanilla" JavaScript way to redirect a page:

window.location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

The following section is for those using HTTP_REFERER as one of many secure measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can be helpful in telling whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)

Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
    var ua        = navigator.userAgent.toLowerCase(),
        isIE      = ua.indexOf('msie') !== -1,
        version   = parseInt(ua.substr(4, 2), 10);

    // Internet Explorer 8 and lower
    if (isIE && version < 9) {
        var link = document.createElement('a');
        link.href = url;

    // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
    else { 
        window.location.href = url; 
Nadeem Yasin
Nadeem Yasin Reply to 2017-01-21 20:07:34Z

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.

Peter Mortensen
Peter Mortensen Reply to 2014-10-26 10:44:31Z

On your click function, just add:

window.location.href = "The URL where you want to redirect";
    window.location.href = "http://www.google.com";
Yauhen Yakimovich
Yauhen Yakimovich Reply to 2018-01-18 15:42:18Z

I also think that location.replace(URL) is the best way, but if you want to notify the search engines about your redirection (they don't analyze JavaScript code to see the redirection) you should add the rel="canonical" meta tag to your website.

Adding a noscript section with a HTML refresh meta tag in it, is also a good solution. I suggest you to use this JavaScript redirection tool to create redirections. It also has Internet Explorer support to pass the HTTP referrer.

Sample code without delay looks like this:

<!-- Place this snippet right after opening the head tag to make it work properly -->

<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<link rel="canonical" href="https://yourdomain.com/"/>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.com/">
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.com/";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
        document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
        var referLink = document.createElement("a");
        referLink.href = url;
    else { window.location.replace(url); } // All other browsers
<!-- Credit goes to http://insider.zone/ -->
Peter Mortensen
Peter Mortensen Reply to 2014-04-03 21:20:11Z

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.

Peter Mortensen
Peter Mortensen Reply to 2014-04-03 21:21:54Z

Write the below code after the PHP, HTML or jQuery section. If in the middle of the PHP or HTML section, then use the <script> tag.

location.href = "http://google.com"
Peter Mortensen
Peter Mortensen Reply to 2014-04-03 21:22:34Z

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

Peter Mortensen
Peter Mortensen Reply to 2014-04-03 21:23:23Z

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').

Peter Mortensen
Peter Mortensen Reply to 2014-10-26 10:55:01Z

So, the question is how to make a redirect page, and not how to redirect to a website?

You only need to use JavaScript for this. Here is some tiny code that will create a dynamic redirect page.

    var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
    if( url ) window.location.replace(url);

So say you just put this snippet into a redirect/index.html file on your website you can use it like so.


And if you go to that link it will automatically redirect you to stackoverflow.com.

Link to Documentation

And that's how you make a Simple redirect page with JavaScript


There is also one thing to note. I have added window.location.replace in my code because I think it suits a redirect page, but, you must know that when using window.location.replace and you get redirected, when you press the back button in your browser it will not got back to the redirect page, and it will go back to the page before it, take a look at this little demo thing.


The process: store home => redirect page to google => google

When at google: google => back button in browser => store home

So, if this suits your needs then everything should be fine. If you want to include the redirect page in the browser history replace this

if( url ) window.location.replace(url);


if( url ) window.location.href = url;
stites Reply to 2016-01-02 04:29:32Z

Try this:


Code snippet of example.

Milap Reply to 2016-02-23 06:26:07Z

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>

RevanthKrishnaKumar V.
RevanthKrishnaKumar V. Reply to 2015-08-13 08:20:46Z

You can use it like in the following code where getGuestHouseRequestToForward is the request mapping (URL). You can also use your URL.

function savePopUp(){
        data: $("#popForm").serialize(),
        dataType: "json",
        error: (function() {
            alert("Server Error");
    success: function(map) {
        window.location = "getGuestHouseRequestToForward";

This is for the same context of the application.

If you want to use only jquery specific code then following code may help:

Ondrej Reply to 2017-10-24 09:52:54Z

JavaScript provides you many methods to retrieve and change the current URL which is displayed in browser's address bar. All these methods uses the Location object, which is a property of the Window object. You can create a new Location object that has the current URL as follows..

var currentLocation = window.location;

Basic Structure of a URL


  1. Protocol -- Specifies the protocol name be used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))

  2. hostname -- Host name specifies the host that owns the resource. For example, www.stackoverflow.com. A server provides services using the name of the host.

  3. port -- A port number used to recognize a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.

  4. pathname -- The path gives info about the specific resource within the host that the Web client wants to access. For example, stackoverflow.com/index.html.

  5. query -- A query string follows the path component, and provides a string of information that the resource can utilize for some purpose (for example, as parameters for a search or as data to be processed).

  6. hash -- The anchor portion of a URL, includes the hash sign (#).

With these Location object properties you can access all of these URL components

  1. hash -Sets or returns the anchor portion of a URL.
  2. host -Sets or returns the hostname and port of a URL.
  3. hostname -Sets or returns the hostname of a URL.
  4. href -Sets or returns the entire URL.
  5. pathname -Sets or returns the path name of a URL.
  6. port -Sets or returns the port number the server uses for a URL.
  7. protocol -Sets or returns the protocol of a URL.
  8. search -Sets or returns the query portion of a URL

Now If you want to change a page or redirect the user to some other page you can use the href property of the Location object like this

You can use the href property of the Location object.

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

Location Object also have these three methods

  1. assign() -- Loads a new document.
  2. reload() -- Reloads the current document.
  3. replace() -- Replaces the current document with a new one

You can use assign() and replace methods also to redirect to other pages like these



How assign() and replace() differs -- The difference between replace() method and assign() method(), is that replace() removes the URL of the current document from the document history, means it is not possible to use the "back" button to navigate back to the original document. So Use the assign() method if you want to load a new document, andwant to give the option to navigate back to the original document.

You can change the location object href property using jQuery also like this


And hence you can redirect the user to some other url.

Ashish Ratan
Ashish Ratan Reply to 2017-06-14 12:57:42Z

You need to put this line in your code


If u are not having the Jquery, go with javascript with

Peter Mortensen
Peter Mortensen Reply to 2016-10-30 16:19:30Z


// 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')
Jaydeep Jadav
Jaydeep Jadav Reply to 2014-01-29 10:20:32Z
<script type="text/javascript">
var url = "https://yourdomain.com";

// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
    var referLink = document.createElement("a");
    referLink.href = url;

// All other browsers
else { window.location.replace(url); }
Sakthi Karthik
Sakthi Karthik Reply to 2016-08-31 12:08:08Z

# 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";.

dnxit Reply to 2014-02-07 20:16:40Z

This is how I use it.

   // If you're on root and redirection page is also on the root

   window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');

   // If you're in sub directory and redirection page is also in some other sub directory.
Community Reply to 2017-05-23 11:47:32Z

Should just be able to set using window.location.


window.location = "http://www.stackoverflow.com";

Here is a past post on the subject:

How to redirect to another webpage in JavaScript/jQuery?

Ben Reply to 2015-01-19 00:58:45Z

There are three main ways to do this,




The last one is best, for a traditional redirect, because it will not save the page you went to before being redirected in your search history. However, if you just want to open a tab with JavaScript, you can use any of the above.1

EDIT: The window prefix is optional.

Peter Mortensen
Peter Mortensen Reply to 2016-07-03 05:09:09Z

Before I start, jQuery is a JavaScript library used for DOM manipulation. So you should not be using jQuery for a page redirect.

A quote from Jquery.com:

While jQuery might run without major issues in older browser versions, we do not actively test jQuery in them and generally do not fix bugs that may appear in them.

It was found here: https://jquery.com/browser-support/

So jQuery is not an end-all and be-all solution for backwards compatibility.

The following solution using raw JavaScript works in all browsers and have been standard for a long time so you don't need any libraries for cross browser support.

This page will redirect to Google after 3000 milliseconds

<!DOCTYPE html>
        <p>You will be redirected to google shortly.</p>
                window.location.href="http://www.google.com"; // The URL that will be redirected too.
            }, 3000); // The bigger the number the longer the delay.

Different options are as follows:

window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL

window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"

When using replace, the back button will not go back to the redirect page, as if it was never in the history. If you want the user to be able to go back to the redirect page then use window.location.href or window.location.assign. If you do use an option that lets the user go back to the redirect page, remember that when you enter the redirect page it will redirect you back. So put that into consideration when picking an option for your redirect. Under conditions where the page is only redirecting when an action is done by the user then having the page in the back button history will be okay. But if the page auto redirects then you should use replace so that the user can use the back button without getting forced back to the page the redirect sends.

You can also use meta data to run a page redirect as followed.

META Refresh

<meta http-equiv="refresh" content="0;url=http://evil.com/" />

META Location

<meta http-equiv="location" content="URL=http://evil.com" />

BASE Hijacking

<base href="http://evil.com/" />

Many more methods to redirect your unsuspecting client to a page they may not wish to go can be found on this page (not one of them is reliant on jQuery):


I would also like to point out, people don't like to be randomly redirected. Only redirect people when absolutely needed. If you start redirecting people randomly they will never go to your site again.

The next part is hypothetical:

You also may get reported as a malicious site. If that happens then when people click on a link to your site the users browser may warn them that your site is malicious. What may also happen is search engines may start dropping your rating if people are reporting a bad experience on your site.

Please review Google Webmaster Guidelines about redirects: https://support.google.com/webmasters/answer/2721217?hl=en&ref_topic=6001971

Here is a fun little page that kicks you out of the page.

<!DOCTYPE html>
        <title>Go Away</title>
        <h1>Go Away</h1>
            }, 3000);

If you combine the two page examples together you would have an infant loop of rerouting that will guarantee that your user will never want to use your site ever again.

Peter Mortensen
Peter Mortensen Reply to 2016-07-03 05:01:56Z

You can redirect in jQuery like this:

$(location).attr('href', 'http://yourPage.com/');
Peter Mortensen
Peter Mortensen Reply to 2014-11-29 06:23:18Z

Here is the code to redirect to some other page with a timeout of 10 seconds.

    function Redirect()

    document.write("You will be redirected to a new page in 10 seconds.");
    setTimeout('Redirect()', 10000);

You can also do it like this, on click of a button using location.assign:

<input type="button" value="Load new document" onclick="newPage()">
    function newPage() {
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.495079 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO