从index.html随机重定向到另一个文件 [英] Redirecting randomly from index.html to another file
问题描述
每次一个用户来到我的主页,即索引
文件,我想要一个脚本运行,所以不同的每次都会查看我网站的随机页面。
Every time a user comes to my home page i.e. the index
file I would like a script to run so a different and random page of my website is viewed each time.
我更喜欢在Javascript或PHP中执行此操作。我想象的索引文件的伪代码看起来像这样:
I would prefer to do this in either Javascript or PHP. The pseudo code of the index file I would imagine would look something like this:
var randomNumber = functionThatReturnsRandomNumber(10);
var urlRedirect;
if (randomNumber == 0)
urlRedirect = 'xxxx.com/folder0/index.html
if (randomNumber == 1)
urlRedirect = 'xxxx.com/folder1/index.html
if (randomNumber == 2)
urlRedirect = 'xxxx.com/folder2/index.html
...
if (randomNumber == 9)
urlRedirect = 'xxxx.com/folder9/index.html
然后是一些将浏览器重定向到 urlRedirect的代码。
and then some code that redirects the browser to urlRedirect.
有什么想法?
编辑
我想我需要成为更明确。有人请建议我如何才能完成上述任务?谢谢。
I guess I need to be more explicit. Would someone please suggest how I could accomplish the above? Thanks.
推荐答案
如果您打算使用Javascript,请使用 var randomnumber = Math.floor( Math.random()* 11);
生成1到10之间的随机数。然后使用 window.location.href = urlRedirect;
将用户重定向到您选择的页面。
If you're going to use Javascript, use var randomnumber=Math.floor(Math.random()*11);
to generate the random number between 1 and 10. And then use window.location.href=urlRedirect;
to redirect the user to your chosen page.
这篇关于从index.html随机重定向到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!