随机linkjavascript忽略当前页面
Random link javascript to ignore current page
当我点击我的导航栏 (bootstrap) 上的 'Random' link 时,它指的是 Javascript 代码(下方)将我带到我的模拟中的随机页面网站。
但是我想添加一个功能,所以如果我打开,说 Link[2]=fed-rates.html
,当我按下导航栏上的 'Random' link 时,它总是带我离开从我当前所在的页面(也就是说,它忽略 Link[2])。
我想知道这是否可行,如果能得到一些想法会很棒。
Javascript代码:
function randomlinks(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="articles/how-to-trade-oil.html"
links[1]="articles/usd-yen-gbp.html"
links[2]="articles/fed-rates.html"
window.location=links[myrandom]
}
// above is for all web pages
function randomlinksarticle(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="how-to-trade-oil.html"
links[1]="usd-yen-gbp.html"
links[2]="fed-rates.html"
window.location=links[myrandom]
}
// above is so navbar link still works on the linked pages, with the way I have the folder directory setup
出现“/undefined”页面的新代码:
function randomlinksarticle(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="how-to-trade-oil.html"
links[1]="usd-yen-gbp.html"
links[2]="fed-rates.html"
links.forEach(function(link, index) {
if (location.href.indexOf(link) !== -1) {
links.splice(index, 1);
}});
window.location=links[myrandom]
您可以执行以下操作:
links.forEach(function(link, index) {
if (location.href.indexOf(link) !== -1) {
links.splice(index, 1);
}
});
这会查看所有 link 并检查它们是否存在于当前 URL 中。如果有,它会使用 splice 函数将它们从列表中删除。
在设置 link[2] 后添加此代码,因为此时应该删除当前页面。
编辑:我还注意到您的随机函数分布不均。这并不是那么重要,但它可能会给你带来麻烦。这样做的原因是 0 和 2 之间舍入到 1 的数字比舍入到 0 或 2 的数字多得多。为了从您的随机数范围中获得零,Math.random() 必须小于 0.5。同样,它必须大于或等于 1.5 才能得到 2。0 和 2 的概率为 0.5/2 或 1/4。这样得到 1 的概率为 1/2,这是有道理的,因为所有数字介于 0.5 和 1.5 之间会给你一个 1。
tl;dr:使用 math.floor(Math.random() * (maximum + 1))
而不是 Math.round(Math.random() * maximum)
来生成随机数。
此外,如果您想要一种重复性较低的方法来执行此操作,您可以将这两个函数替换为如下内容:
function randomLink() {
var links = Array.prototype.slice.call(arguments, 0); //Turns the function's arguments into an array
links.forEach(function(link, index) { //Loops through all the links
if (location.href.indexOf(link) !== -1) { //If the link text is contained in the url
links.splice(index, 1); //Remove the link from the links array
}
});
var rand = Math.floor(Math.random() * links.length); //Choose a number between 0 and links.length - 1
window.location = links[rand]; //Visit the link
}
您可以将其称为 randomLink("first_page.html", "second_page.html")
任意页数。
当我点击我的导航栏 (bootstrap) 上的 'Random' link 时,它指的是 Javascript 代码(下方)将我带到我的模拟中的随机页面网站。
但是我想添加一个功能,所以如果我打开,说 Link[2]=fed-rates.html
,当我按下导航栏上的 'Random' link 时,它总是带我离开从我当前所在的页面(也就是说,它忽略 Link[2])。
我想知道这是否可行,如果能得到一些想法会很棒。
Javascript代码:
function randomlinks(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="articles/how-to-trade-oil.html"
links[1]="articles/usd-yen-gbp.html"
links[2]="articles/fed-rates.html"
window.location=links[myrandom]
}
// above is for all web pages
function randomlinksarticle(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="how-to-trade-oil.html"
links[1]="usd-yen-gbp.html"
links[2]="fed-rates.html"
window.location=links[myrandom]
}
// above is so navbar link still works on the linked pages, with the way I have the folder directory setup
出现“/undefined”页面的新代码:
function randomlinksarticle(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="how-to-trade-oil.html"
links[1]="usd-yen-gbp.html"
links[2]="fed-rates.html"
links.forEach(function(link, index) {
if (location.href.indexOf(link) !== -1) {
links.splice(index, 1);
}});
window.location=links[myrandom]
您可以执行以下操作:
links.forEach(function(link, index) {
if (location.href.indexOf(link) !== -1) {
links.splice(index, 1);
}
});
这会查看所有 link 并检查它们是否存在于当前 URL 中。如果有,它会使用 splice 函数将它们从列表中删除。
在设置 link[2] 后添加此代码,因为此时应该删除当前页面。
编辑:我还注意到您的随机函数分布不均。这并不是那么重要,但它可能会给你带来麻烦。这样做的原因是 0 和 2 之间舍入到 1 的数字比舍入到 0 或 2 的数字多得多。为了从您的随机数范围中获得零,Math.random() 必须小于 0.5。同样,它必须大于或等于 1.5 才能得到 2。0 和 2 的概率为 0.5/2 或 1/4。这样得到 1 的概率为 1/2,这是有道理的,因为所有数字介于 0.5 和 1.5 之间会给你一个 1。
tl;dr:使用 math.floor(Math.random() * (maximum + 1))
而不是 Math.round(Math.random() * maximum)
来生成随机数。
此外,如果您想要一种重复性较低的方法来执行此操作,您可以将这两个函数替换为如下内容:
function randomLink() {
var links = Array.prototype.slice.call(arguments, 0); //Turns the function's arguments into an array
links.forEach(function(link, index) { //Loops through all the links
if (location.href.indexOf(link) !== -1) { //If the link text is contained in the url
links.splice(index, 1); //Remove the link from the links array
}
});
var rand = Math.floor(Math.random() * links.length); //Choose a number between 0 and links.length - 1
window.location = links[rand]; //Visit the link
}
您可以将其称为 randomLink("first_page.html", "second_page.html")
任意页数。