[Solved -10 Answers]How to redirect to another webpage in JavaScript/jQuery

How to redirect the user from one page to another using javascript or 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.


  • To simulate someone clicking on a link, use location.href
  • To simulate an HTTP redirect, use location.replace
javascript code
// similar behavior as an HTTP redirect

// similar behavior as clicking on a link
window.location.href = "";


javascript code
$(location).attr('href', '')

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.

Use: redirect(‘anotherpage.aspx’);

javascript code
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)
        window.location.href = url; 

  • You can use this Code:
javascript code
// window.location
window.location.href = ''
document.location.href = '/path'

// window.history

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

// Probably no bueno
self.location = '';
top.location = '';

// jQuery
$(location).prop('href', '')

  • To works for each browser:
javascript code
window.location.href = 'your_url';

  • Redirect to another page, you can use:
javascript code
window.location = "";

  • You can’t redirect to a function. What you can do is pass some flag on the URL when redirecting, then check that flag in the server side code and if raised, execute the function.
javascript code
document.location = "MyPage.php?action=DoThis";

  • HTML Example for Redirecting a Page or URL
  • Here is complete HTML page, which uses method to redirect a page or URL. Since you can only use one method at a time.
html code
<!DOCTYPE html>
                <title>JavaScript and JQuery Example to Redirect a page or URL </title>
                <div id="redirect">
                        <h2>Redirecting to another Page</h2>

                <script src="scripts/jquery-1.6.2.min.js"></script>

                   // JavaScript code to redirect a URL
                     // Another way to redirect page using JavaScript

                      //JQuery code to redirect a page or URL
                         /var url = "";

  • This works with jQuery:
javascript code
$(window).attr("location", "");

  • HTML Page Redirect Using jQuery/JavaScript
javascript code
function YourJavaScriptFunction()
    var i = $('#login').val();
    if (i == 'login')
        window.location = "login.php";
        window.location = "Logout.php";

If you need to give a complete URL as window.location=””;

READ  JAVASCRIPT - What is the difference between Bower and npm

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.