Friday, January 31, 2020

Learn the various ways in which JavaScript can be used to redirect a web page to another


Hello guys evening and welcome to my new tutorial post on JavaScript. In my previous tutorial post we discussed about JavaScript Cookies.

In this tutorial post we will be discussing about JavaScript Page Redirection. Feel free to ask your questions via the comment box in any area you don't understand properly, your questions will be attended to as soon as possible.

What is Page Redirection?

You might have encountered a situation where you clicked a URL to reach a page X but internally you where directed to another page Y. It happens due to page redirection. This concept is different from JavaScript Page Refresh.

There could be various reasons why you would like to redirect a user from the original page. We are listing down a few of the reasons-

  • You did not like the name of your domain and you are moving to a new one. In such a scenario, you may want to direct all your visitors to the new site. Here you can maintain your old domain but put a single page with a page redirection such that all your old domain visitors can come to your new domain.
  • The Search Engines may have already indexed your pages. But while moving to another domain, you would not like to lose your visitors coming through  search engines. So you can use client-side page redirection. But keep in mind this should not be done to fool the search engine,it could lead your site to get banned.

How Page Redirection Works?

The following examples below explains or demonstrates how to implement page Redirections.

Example 1

It is quite simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows.

<html>
     <head>
          <script type = "text/Javascript">
               <!--
                    function Redirect() {
                         window.location = "https://webdesigntutorialz.com";       
                    }   
               //-->
          </script>
     </head>

     <body>
          <p>Click the following button, you will be redirected to home page.</p>        

          <form>
               <input  type = "button"  value = "Redirect"  onclick = "Redirect();" />
          </form>

     </body>
</html>

Output

Below is the output of the above example.

Click the following button, you will be redirected to home page. 

 


Example 2

You can show an appropriate message to your site visitors before redirecting them to a new page. This would need a bit time delay to load a new page. The following example shows how to implement the same. Here setTimeout() is a built-in JavaScript function which can be used to execute another function after a given time interval.

<htm>
     <head>
          <script  type = "text/JavaScript">
               <!--
                    function  Redirect() {
                         window.location = "https://webdesigntutorailz.com";          
                    }
                    document.write("You will be redirected to main page in 10 sec.");
                    setTimeout('Redirect()' ,  10000);
               //-->
          </script>
     </head>

     <body>
     </body>
</html>

Output
You will be redirected to main page in 10 sec.

Example 3
The following example shows how to redirect your site visitors onto a different page based on their browsers.

<html>
     <head>
          <script  type = "text/Javascript">
               <!--
                    var  browsername = navigator . appName;
                    if ( browsername == "Netscape" ) {
                         window.location = "https://webdesigntutorailz.com";     
                    } else if ( browser == "Microsoft Internet Explorer" ) {
                         window.location = "https://google.com";
                    } else  {
                         window.location = "https://ebay.com";
                    }
               //-->
          </script>
     </head>

     <body>
     </body>
</html>

Alright guys we have come to the end of this tutorial post on JavaScript page redirection. In my next tutorial post i will be talking about JavaScript dialog boxes.

Always feel free to ask questions in areas you don't understand properly, and your questions will be attended to as soon as possible. Follow us on our various social media handles to stay updated on our latest tutorials, thank you and bye for now.
Share:

0 comments:

Post a Comment