rolory
rolory

Reputation: 362

How to redirect a webpage to another webpage by random number?

Just wanted to know how by random number can I redirect a webpage to another webpage.
In HTML I would do it like that way:

<meta http-equiv="refresh" content="seconds; url=http://example.com/" />

If the max range is 1.5 and the min range is 0.5, (from 0.5 to 1.5) for example, how can I insert it instead the "seconds" value? Simple quite question.
Thanks.

Upvotes: 0

Views: 396

Answers (3)

Exlord
Exlord

Reputation: 5381

var second = parseInt(Math.random()* (1500 - 500) + 500);
setTimeout(function(){ window.location = "http://example.com/" },second);

UPDATED mistaken js random to php rand this one works tested in http://jsfiddle.net/3SNxg/

Upvotes: 4

mujtaba_ahmad
mujtaba_ahmad

Reputation: 402

You could try the following :

var meta_refresh = document.createElement("meta");
meta_refresh.setAttribute("http-equiv","refresh");
var random_delay = 0.5 + Math.random();
meta_refresh.setAttribute("content",random_delay+"; url=http://example.com/");
document.getElementsByTagName("head")[0].appendChild(meta_refresh);

Upvotes: 0

user3077503
user3077503

Reputation:

that is very easy.

just set the content to empty and then using jquery:

randomSecond = Math.random(2, 5); // ranges from 2 to 5 seconds

$('#metaID').prop("content", (randomSecond+"; myLinktoThatPage")  );

This approach is of course used in case you want to fill the meta tag, but there is window.location.href used in conjunction with setTimeout that can help you to redirect the page in raw JS.

But if you do not use jQuery and need raw Javascript:

randomSecond = Math.random(2, 5); // ranges from 2 to 5 seconds

document.getElementById('MyMetaTagID').setAttribute("content", randomSecond+"; myLinktoThatPage");

Upvotes: 1

Related Questions