【问题标题】:Display Random Webpage From Selection从选择中显示随机网页
【发布时间】:2015-07-08 15:11:19
【问题描述】:
我找不到完全解决我的问题的现有答案。
我有一个域http://bonfiredog.co.uk,它有一个index.html。我还有一些其他 HTML 格式的网页;让我们称呼他们:
splash1.html
splash2.html
splash3.html
我希望人们在访问我的域时随机显示其中一个网页。
我假设我可以通过在 index.html 中运行 jQuery 脚本来做到这一点,但我似乎无法对解决方案进行逆向工程。有人可以帮忙吗?
【问题讨论】:
标签:
javascript
jquery
html
random
【解决方案1】:
你可以这样做:
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];
【解决方案2】:
创建一个位置数组,然后从数组中生成一个随机索引,获取随机索引处的项目并重定向到该位置。
以下功能应该适合您:
$(function() {
var targets = ['splash1.html', 'splash2.html', 'splash3.html'];
var index = Math.floor(Math.random() * targets.length);
window.location = targets[index];
});