Javascript Page Redirect
You may have come across a situation where you clicked a URL to go to page X but were instead led to page Y. It occurs as a result of page redirection. This is not the same as the JavaScript Page Refresh concept.
You may want to redirect a user away from the original page for a variety of reasons. We've assembled a list of a number of them.
- You've chosen to change the name of your domain because you don't like it. You will want to guide all of your visitors to the new site in this situation. You may keep your old domain but create a single page with a page redirection so that all visitors to your old domain can go to your new domain.
- If you've created several pages based on browser versions, titles, or even different countries, you can use client-side page redirection instead of server-side page redirection to guide your users to the appropriate page.
- It's likely that the search engines have already indexed your pages. However, you don't want to lose visitors that came from search engines when switching to a new domain. As a consequence, client-side page redirection is an option. However, bear in mind that this can not be done to deceive the search engine; otherwise, the site could be blocked.
How Does It Work?
A page redirect using JavaScript on the client side is very easy. To redirect your site visitors to a new tab, simply add the following line to your head section.
<html>
<head>
<script type = "text/javascript">
function redirect() {
window.location = "https://www.websitesiq.com";
}
</script>
</head>
<body>
<p>When you click on the following button, you will be redirected:
<form>
<input type = "button" value="Redirect Me" onclick ="redirect();" />
</form>
</body>
</html>
Live Demo!
Output
When you click on the following button, you will be redirected: