Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示所选内容中的随机网页_Javascript_Jquery_Html_Random - Fatal编程技术网

Javascript 显示所选内容中的随机网页

Javascript 显示所选内容中的随机网页,javascript,jquery,html,random,Javascript,Jquery,Html,Random,我找不到一个解决我问题的现有答案 我有一个域名,http://bonfiredog.co.uk,它有一个索引.html。我还有一些HTML格式的网页;我们叫他们: splash1.html splash2.html splash3.html 我希望人们在访问我的域名时,能够随机显示其中一个网页 我假设我可以通过在index.html中运行jQuery脚本来实现这一点,但我似乎无法对解决方案进行反向工程。有人能帮忙吗?创建一个位置数组,然后从数组中生成一个随机索引,从随机索引中获取项目并重定

我找不到一个解决我问题的现有答案

我有一个域名,
http://bonfiredog.co.uk
,它有一个
索引.html
。我还有一些HTML格式的网页;我们叫他们:

splash1.html 
splash2.html 
splash3.html
我希望人们在访问我的域名时,能够随机显示其中一个网页


我假设我可以通过在index.html中运行jQuery脚本来实现这一点,但我似乎无法对解决方案进行反向工程。有人能帮忙吗?

创建一个位置数组,然后从数组中生成一个随机索引,从随机索引中获取项目并重定向到该位置

以下功能应适用于您:

$(函数(){
var targets=['splash1.html','splash2.html','splash3.html'];
var index=Math.floor(Math.random()*targets.length);
window.location=目标[索引];
});

您可以这样做:

var redirectUrls = [
'http://bonfiredog.co.uk/splash1.html',
'http://bonfiredog.co.uk/splash2.html',
'http://bonfiredog.co.uk/splash3.html',
]

var randomNumber = Math.floor(Math.random() *3);

location.href = redirectUrls[randomNumber];

谢谢你的帮助!