Javascript 我的随机链接有问题
我有一个非常简单的页面(整个内容复制如下),在加载时,会随机将用户重定向到7篇文章中的一篇。一篇文章(链接列表中的最后一篇)每次都会导致404错误,我不知道为什么。复制并粘贴到浏览器中时,链接可以正常工作。任何能指出问题所在的人都会很好,谢谢Javascript 我的随机链接有问题,javascript,html,random,Javascript,Html,Random,我有一个非常简单的页面(整个内容复制如下),在加载时,会随机将用户重定向到7篇文章中的一篇。一篇文章(链接列表中的最后一篇)每次都会导致404错误,我不知道为什么。复制并粘贴到浏览器中时,链接可以正常工作。任何能指出问题所在的人都会很好,谢谢 <!DOCTYPE HTML> <html> <head> <!-- Global site tag (gtag.js) - Google Analytics --> <script a
<!DOCTYPE HTML>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-34602317-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-34602317-1');
</script>
<title>Words That Kinda Matter</title>
<meta charset="utf-8" />
<script type="text/javascript">
var pageArr = ["https://medium.com/@olivershiny/eb47cffd04f1", "https://medium.com/@manfraiya/a2a3fcfd046c", "https://medium.com/@sravss/43f43d67593c", "https://medium.com/@rachaelflanery/9d457ba9a357", "https://medium.com/@benjaminsledge/9a19b7f85dfb", "https://medium.com/@writingsolo/7dac9351cd57", "https://medium.com/@justincox/46342de79f68"];
document.location.href = pageArr[Math.ceil(Math.random()*7)];
</script>
</head>
<body>
</body>
window.dataLayer=window.dataLayer | |[];
函数gtag(){dataLayer.push(参数);}
gtag('js',新日期());
gtag(“配置”、“UA-34602317-1”);
有点关系的话
var pageArr=[”https://medium.com/@olivershiny/eb47cffd04f1“https://medium.com/@manfraiya/a2a3fcfd046c“https://medium.com/@sravss/43f43d67593c“https://medium.com/@rachaelflanery/9d457ba9a357英寸https://medium.com/@Benjaminslage/9a19b7f85dfb“https://medium.com/@写入SOLO/7dac9351cd57“https://medium.com/@justincox/46342de79f68“];
document.location.href=pageArr[Math.ceil(Math.random()*7)];
而不是
document.location.href = pageArr[Math.ceil(Math.random()*7)];
你需要的是
document.location.href = pageArr[Math.floor(Math.random()*7)];
使用
ceil
时,最后一项将始终不存在,因为它将等于数组的长度。在数组中,索引从0开始。因此,您需要使用floor
Math.ceil(Math.random()*7)
可以是7
,它是数组的长度。这就成功了。谢谢你,谢谢你的解释!