javascript - 尝试设置随机重定向主页?
问题描述
我正在帮助我的朋友建立一个网站,我正在尝试对其进行设置,以便主页随机重定向到其中一个页面,现在设置为三个,但能够添加更多。
目前它似乎在某些时候有效,但有时不会通过,而且它似乎比其他房间更容易重定向到其中一个房间?
<script type="text/javascript">
//RANDOMLY REDIRECTS TO A ROOM !
var doors = Math.floor(Math.random() * 3) + 1;
if (doors == 1) {
window.location.href =
"http://sarahboulton.co.uk/kitchen.html";
}
else if (doors == 2) {
window.location.href =
"http://sarahboulton.co.uk/livingroom.html";
}
else if (doors == 3) {
window.location.href =
"http://sarahboulton.co.uk/chapelbedroom.html";
}
</script>
解决方案
使用文件名创建一个数组。选择一个随机数,它应该是数组的索引。这是代码
<script type="text/javascript">
//RANDOMLY REDIRECTS TO A ROOM !
var pages = [ 'Kitchen', 'livingroom', 'chapelbedroom' ];
var doors = Math.floor(Math.random() * ( pages.length - 1 ));
var url = 'http://sarahboulton.co.uk/' + pages[doors] + '.html';
window.location = url;
</script>
推荐阅读
- rules - 如何在 CLIPS 中添加多个变量?
- python - .lower() 出现 0 时返回 True
- r - 使用 xml 抓取网页价格
- python - Python 中的 DAG 实现
- javascript - 如何将我的 JSON 结构更改为 chartJS 条形图 JSON 结构?
- glsl - 水着色器的问题(不想在水面上折射物体)
- python - 如何计算两个数据帧中所有行之间的 Levenshtein 距离并输出每对的 Levenshtein 分数?
- java - 如何使用 okta java sdk 从 okta 应用程序获取客户端密码
- azure - 天蓝色管道 yaml 配置
- javascript - Enzyme 如何检查组件的可见性?