English 中文(简体)
Random URL JS with Multi Lists
原标题:Random URL JS with Multiple Lists

I have a navigation bar and I want to access a random URL from a distinct list associated with each link. Link A would access List A, link B would access List B, and so on. I have been using a javascript code from 1997 that works great for one link:

载于js文档中的“设计”;

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

var item = 0;
var URL = new Array();

URL[item++] =  listitema.php ;
URL[item++] =  listitemb.php.php ;
URL[item++] =  listitemc.php ;

function randomJump() {
    var random = rand(item) - 1;
    location.href = URL[random];
};

<-end js file:->

查阅:

<a href="javascript:randomJump()" onmouseover="self.status=  ;return true">

While this code works well for one link per page, I now need to have it work for multiple links, each with a different reference list. I have no idea how to make this a script that works for each link without interfering with other links. Thank you.

最佳回答

这里的职能范围很小,与许多不同的URL清单合作,正如你想要的那样:

var listA = [
     listitem-a.php ,
     listitem-b.php ,
     listitem-c.php 
];

var listB = [
     listitem-1.php ,
     listitem-2.php ,
     listitem-3.php 
];

var listC = [
     listitem-x.php ,
     listitem-y.php ,
     listitem-z.php 
];

function goRandomURL(list) {
    window.location = list[Math.floor(Math.random() * list.length)];
}

然后,为了使用<代码>listA,请:

goRandomURL(listA);

或将其置于这样的联系之中:

<a href="#" onclick="goRandomURL(listA)">

或者为了名单B,你将使用:

<a href="#" onclick="goRandomURL(listB)">

单凭“javascriptMath.random(),在0到1之间(不包括1个)作为浮动体的随机回归,并且根据当地环境已经播种,因此,你无需介绍自己的种子或随机功能。 然后,随机数量按阵列的长度加以缩小,然后四舍五入到阵列指数的整数。 这产生了对我们阵列选择随机指数的效果。 它自动跟踪阵列的长度,以便你可以自由地从阵列中增加/删除物品,而不会以任何方式改变守则。


EDIT - to help you on your current code. In your current code, I see this:

var listA = [
     orange.php ;
     red.php ;
     blue.php ;
];

var listB = [
     gold.php ;
     silver.php ;
     bronze.php ;
];

var listC = [
     olive.php ;
     pink.php ;
     purple.php ;
];

应当:

var listA = [
     orange.php ,
     red.php ,
     blue.php 
];

var listB = [
     gold.php ,
     silver.php ,
     bronze.php 
];

var listC = [
     olive.php ,
     pink.php ,
     purple.php 
];

而且:

goRandomURL(list) {
    window.location = list[Math.floor(Math.random() * list.length)];
}

应:

function goRandomURL(list) {
    window.location = list[Math.floor(Math.random() * list.length)];
}
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签