如何在Javascript中选择一个随机单词

时间:2013-09-23 16:06:03

标签: javascript

这个脚本应该从可用字符中选择一个随机单词并添加它添加url的结尾但它似乎不起作用。你能确定我在哪里解决问题吗?

    <script type="text/javascript">
    function randomEI1(len) {
var chars = [ "apple", "google", "ebay" ];
 var string_length = len;
 var randomstring ='';
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
}

var ei1 = randomEI1(1);

document.write('<script type="text/javascript" src="http://www.site.com' + ei1 + '"><\/script>');

</script>

1 个答案:

答案 0 :(得分:0)

function randomEI1() {
   var chars = [ "apple", "google", "ebay" ];
   return chars[ Math.floor(Math.random() * chars.length)];
}
var ei1 = randomEI1();
document.write('<script type="text/javascript" src="http://www.site.com' + ei1 + '"> <\/script>');

我不确定你用len参数做了什么。这是否符合您的需求?