How to redirect to another webpage in Javascript or JQuery

       Some times we need to redirect an user from one page to another page. In such a case you can either use javascript or jquery to navigate the user to another page. In this article let us check out how to redirect to another webpage in javascript and jquery as well.

redirect to another webpage in javascript

Redirect to another webpage in JavaScript
       This is the concept of page redirection. We make use of Window.Location inorder to redirect an user from the current page to some other page. Let us consider an example on how to use Window.Location in the script tag of html to navigate to some other page.

<script type="text/javascript">
         <!--
            function RedirectToToPoint() {
               window.location.replace("http://www.topoint.in");
            }
         //-->
      </script>

    In the above example, the Window.Location.replace redirects the page to ToPoint.in as the location is specified so. In order to make use of the above method, what ever the page you wanted to redirect your current page, your can do that by specifying the URL in Window.Location.replace.



If you want to do that when an user clicks on some button or a link, you can make use of onclick event to trigger the above function.The similar redirect can be achieved by using the below code when you want the redirect to happen after some action.

<script type="text/javascript">
         <!--
            function RedirectToToPoint() {
               window.location.href="http://www.topoint.in";
            }
         //-->
      </script>

Difference between Window.Location.replace and Window.Location.href is replace doesn’t store the previous session history as a result there will not be any back navigation enabled to the initial page from where the redirection occured. Instead, in href the session history contains the original page from where the user is redirected. Hence, back button will be enabled.

Using JQuery
Using JQuery you can actually redirect from one page to another. This is not search engine friendly as of JavaScript. What JQuery redirect does is nothing but a JavaScript redirect. It is always preferable to go with a javascript redirect rather than JQuery redirect.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
   // jQuery URL redirection
   $(document).ready( function() {
      url = "http://www.topoint.in";
      $( location ).attr("href", url);
   });
</script>

So, finally its better to make use of javascript to perform a redirect rather than jquery. It’s up to you to decide what you gonna use!
More Content : ToPoint
More about JavaScript : JavaScript