从Javascript随机生成器中排除链接

时间:2018-01-25 21:39:30

标签: javascript

有没有人知道如何从random_all javascript生成器中排除特定链接。

我有一个存档博客页面,其中包含一个链接列表,我在其中创建一个随机按钮。但是,下面的随机链接脚本从页面获取任何链接。我想排除一些链接,以便它们不会被包含在脚本中(例如:我的主页,或联系页面......只是页面上显示的博客文章)

http://www.eointhomassharkey.com/tester - 页面可以在这里找到

    <script>
    function random_all(){
    var myrandom=Math.round(Math.random()*(document.links.length-1))
    window.location=document.links[myrandom].href
    } 
    //-->
    </script>

非常感谢任何帮助。

-Eoin Thomas

3 个答案:

答案 0 :(得分:1)

首先,由于Math.random()得到的数字介于0和.9之间,重复(从不为1),因此您希望将其更改为逻辑表达式。然后你运行一个循环......或者更好的是,创建一个inArray函数。如果排除的数量太多,那么您可以使用递归。

&#13;
&#13;
//<![CDATA[
/* external.js */
var doc, bod, I, inArray, randLinker, old; // for use on other loads
onload = function(){
if(old)old(); // change old var if using technique on other pages
doc = document; bod = doc.body;
I = function(id){
  return doc.getElementById(id);
}
inArray = function(v, a){
  for(var i=0,l=a.length; i<l; i++){
    if(a[i] === v){
      return true;
    }
  }
  return false;
}
randLinker = function(){
  var exclude = [].slice.call(arguments), lnx = doc.links;
  var randLink = lnx[Math.floor(Math.random()*lnx.length)];
  var randHref = randLink.href;
  if(randLink === this || inArray(randHref, exclude)){
    return randLinker.apply(this, exclude);
  }
  this.href = randHref;
}
I('frm').onsubmit = function(){
  return false;
}
var rand_link = I('rand_link');
rand_link.onclick = function(){
  randLinker.call(this, 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Text_formatting', 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions');
}
}
//]]>
&#13;
/* external.css */
html,body{
  padding:0; margin:0;
}
body{
  background:#000; overflow-y:scroll;
}
.main{
  width:940px; background:#ccc; padding:20px; margin:0 auto;
}
#frm>a{
  float:left; clear:left;
}
.cb{
  clear:both;
}
&#13;
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='viewport' content='width=device-width' />
    <title>Random Link</title>
    <link type='text/css' rel='stylesheet' href='external.css' />
    <script type='text/javascript' src='external.js'></script>
  </head>
<body>
  <div class='main'>
    <form id='frm' name='frm'>
      <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration'>Loops and iteration</a>
      <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions'>Functions</a>
      <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators'>Expressions and Operators</a>
      <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates'>Numbers and dates</a>
      <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Text_formatting'>Text formatting</a>
      <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions'>Regular Expressions</a>
      <a id='rand_link' href=''>Random Link</a>
    </form>
    <div class='cb'></div>
  </div>
</body>
</html>
&#13;
&#13;
&#13;

请注意randLinker需要任意数量的参数。每个人都将被排除在外。确保在随机链接的上下文中调用此函数。

答案 1 :(得分:0)

我建议您设置一系列您希望排除的网页/网站。然后,您可以运行在console.log()条件内重定向(或在我的示例中运行if)的代码,该条件检查数组是否包含随机生成的链接{{1} }值。

可以使用ES6方法 includes() 来完成此操作。由于您不希望数组包含该值,因此您只需检查href是否返回false。

这可以在下面看到,其中逻辑只会触发StackOverflow和Twitter:

&#13;
&#13;
includes()
&#13;
var exclusions = ['http://www.facebook.com/', 'http://www.google.com/']

function random_all() {
  var myrandom = Math.round(Math.random() * (document.links.length - 1));
  if (!exclusions.includes(document.links[myrandom].href)) {
    //window.location = document.links[myrandom].href;
    console.log(document.links[myrandom].href);
  }
  else {
    console.log('The random link is on the exclusion list!');
  }
}

random_all();
&#13;
&#13;
&#13;

希望这有帮助! :)

答案 2 :(得分:0)

这会过滤掉document.links中所有列入黑名单的链接,然后只进行白名单随机化:

var blacklist = ["javascript:random_all()", "https://google.com/"];

function random_all(){
    var whitelist = Array.from(document.links).filter(item => !blacklist.includes(item.href));
    if (whitelist.length === 0) {
      alert('No whitelisted links!')
      return;
    }
    // 0<->(whitelist.length-1)
    var myrandom=Math.round(Math.random()*(whitelist.length-1))
    console.log(whitelist[myrandom].href);
    //window.location=whitelist[myrandom].href
}
<a href="https://google.com/">Google</a>
<a href="https://stackoverflow.com/">Stackoverflow</a>
<a href="https://github.com/">Github</a>

<a href="javascript:random_all()">Random</a>