Page Redirect To Random URL After Set Time Gap Using JavaScript

Redirecting to another URL or refreshing the page using the <meta http-equiv="refresh" content="30;URL='http://example.com/'" /> is not a big thing. There are hundreds of such easy meta tags that will help you out. But, the challenge comes when you want to redirect to a URL other than your site and that to after certain time gap set by you and the URL to which it is being redirected, should always redirect to a unique URL, not the same URL again and again. Also, there has to be option to redirect to unique URL of different site. For example; you have up to 50 sites or even more. You want to redirect your visitors to randomly to any site of yours from any of the site. This function can not be achieved by the meta http-equiv function. For that you need to apply the below code. You can see this article for more info.

<script language="JavaScript" type="text/javascript">
var sites = ['http://examplesite1.com', 'http://examplesite2.com', 'http://examplesite3.com'] ;
setTimeout("window.location.replace(sites[Math.floor(Math.random()*sites.length)] );",5000);// milliseconds, so 1 seconds = 1000ms
</script>

This is a piece of JavaScript code which will solve the purpose explained in above example. Just insert the code within <head> </head> and you are done. If you want to know more about 301 & 302 redirect please see this guide.

In var sites enter all the URLs you want to redirect within single quotes as shown in the above code. You can add as many URLs you want. In the setTimeout where 5000 is written at the end of the code, replace with the time gap you want to maintain. Remember 1000=1 Second.

How the code works?

When a visitors comes to any of your site, he will spend the amount of time you have set in the code setTimeout. Once the time is triggered, the page will refresh automatically and the visitor will be redirected to a random site in var sites. This will keep happening till the visitor closes the latest site. Unless the visitor closes your site, this will keep refreshing in the time gap and directed to a random site.

Another Fun With This Code

In the above method it will let the visitor land in a specific URL which is mentioned in the var sites. But, this can be made to land in a random page of the site each time a visitors lands. By random page it means, any page within the site, not just same page every time.

To achieve this in a WordPress site, it is super easy.

There is a WordPress Plugin “Redirect URL to Post“. After you install this plugin, just use this piece of extension after the URL. “/?redirect_to=random“. For example; if your site URL is “http://example.com” then the actual URL will be “http://example.com/?redirect_to=random“. With this method, each time a visitor is redirected to a random page in any of your site.

Page Redirect To Random URL After Set Time Gap Using JavaScript

This plugin provides an URL that takes you directly to a post in single-post view. This post is determined by the query parameter ?redirect_to= and optional others. You can, for example, redirect to your latest, oldest or a random post, or to your second latest, or the oldest post that is not older than two months. Simply enter the URL of your WordPress site into your browser and add ?redirect_to=… to the end. You can, of course, also use that URL in any link, in a menu, a button and so on. The plugin will recognize it and redirect the browser to the right post or page.

Support Us By Sharing

Author: Aliva Tripathy

Taking out time from a housewife life and contributing to AxiBook is a passion for me. I love doing this and gets mind filled with huge satisfaction with thoughtful feedbacks from you all. Do love caring for others and love sharing knowledge more than this.

Leave a Reply

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

*

code