Page Navigation (Redirect) in JavaScript
IT Tricks

Page Navigation (Redirect) in JavaScript


What is page redirection?

You may encounter a situation when you click on a URL to get to page X but you are redirected to page Y. It happens because Page RedirectionNavigate the page. This concept is different from: JavaScript – Refresh the page.

There are various reasons why you want to redirect users from the original page. Here are some reasons:

You dislike your domain name and you are trying to move to a new domain. In this case, you may want to direct all your visitors directly to the new site. Here, you can maintain the old domain name, but place a single page with a page redirection so that all old domain visitors can reach the new domain.

You have built different pages based on browsers or their names or maybe based on different countries, then, instead of using page redirection on Server-Side, you can use Client-Side page redirection to redirect the user to the appropriate page.

Search Engines may have indexed your pages. But while moving to a new domain, you don’t want to lose visitors through search. So you can use page redirection on the Client-Side. But you should remember that this should not be done to deceive Search Engine, it can turn your page into a banned site.

Page Redirection how does it work?

The implementation process of Page Redirection is as follows:

Example 1

It is quite simple to implement Page Redirection using JavaScript at the Client-Side. To redirect visitors to a new page, simply add a line of code in the head area as follows:

   type="text/javascript">     

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

type="button" value="Redirect Me" onclick="Redirect();" />

Example 2

You can display an appropriate message to your visitors before navigating them to a new page. This may require some delay to load the new page. The following example shows how to do the same. Here, setTimeout () is a function available in JavaScript that can be used to execute other commands after a given period of time.

   type="text/javascript">      

Result

Actions like above to see results

Example 3

The following example shows how to redirect your visitors to another page based on the browser they use.

   type="text/javascript">      

Follow Tutorialspoint

Previous lesson: Events in JavaScript

Next: Dialog – Alert, Prompt, Confirmation in JavaScript

.

Leave a Reply

Your email address will not be published. Required fields are marked *