生成随机网址,但网址长度因某种原因无法更改?

时间:2016-01-28 14:54:05

标签: javascript html random

截至最近,我一直在网站上工作,这在课堂上是一种爱好。在这个网站上有一些基本文本,一个按钮和一个文本字段。当您按下按钮时,它会生成一个随机URL,然后您可以添加自己的扩展名。目前,他们将人们发送到随机的.onion域名。我目前的问题是我无法将URL的长度设置为随机长度。这是我的代码,如果你可以推荐一个很棒的修复程序。 `



<body>
  <script>
    var 'x' = Math.random() * 20
  </script>
  <script language="javascript" type="text/javascript">
    function randomString() {
      var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
      var string_length = 'x';
      var randomstring = '';
      for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
      }
      document.randform.randomfield.value = randomstring;
    }
  </script>
  <form name="randform">
    <input type="button" value="Generate A New Base URL" onClick="randomString();">&nbsp;
    <input type="text" name="randomfield" value="">
  </form>
</body>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

这在修复后有效:'x' =&gt; x

&#13;
&#13;
        var x = Math.random() * 20;
        function randomString() {
          var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
          var string_length = x;
          var randomstring = '';
          for (var i = 0; i < string_length; i++) {
            var rnum = Math.floor(Math.random() * chars.length);
            randomstring += chars.substring(rnum, rnum + 1);
          }
          document.randform.randomfield.value = randomstring;
        }
&#13;
      <form name="randform">
        <input type="button" value="Generate A New Base URL" onClick="randomString();">&nbsp;
        <input type="text" name="randomfield" value="">
      </form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

        var x = Math.random() * 20;
        function randomString() {
          var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
          var string_length = x;
          var randomstring = '';
          for (var i = 0; i < string_length; i++) {
            var rnum = Math.floor(Math.random() * chars.length);
            randomstring += chars.substring(rnum, rnum + 1);
          }
          document.randform.randomfield.value = randomstring;
        }
      <form name="randform">
        <input type="button" value="Generate A New Base URL" onClick="randomString();">&nbsp;
        <input type="text" name="randomfield" value="">
      </form>