//flex table opened by JP

Click to See Complete Forum and Search --> : A quick java question...please


tinwindow
12-23-2007, 12:41 PM
I got this javascript that i like but without the knowlege I dont know how to
modify it...I want it to move to a link once a mouse clicks but not by random
to the next one untill the end of the list and start over when end is reached
one click at a time...
can anyone here help me modify this script ?
thanx
tw

CODE:

<script>
<!--
/*
Random link button- By JavaScript Kit (http://javascriptkit.com)
Over 300+ free scripts!
This credit MUST stay intact for use
*/

//specify random links below. You can have as many as you want
var randomlinks=new Array()

randomlinks[0]="http://mysite/"
randomlinks[1]="http://mysite/"
randomlinks[2]="http://mysite/"
randomlinks[3]="http://mysite/"
randomlinks[4]="http://mysite"

function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}
//-->
</script>
<form>
<p><input type="button" name="B1" value="Random Link >>" onclick="randomlink()"></p> </form>

<!--Uncomment below to use a regular text link instead
<a href="javascript:randomlink()">Random Link</a>
-->
======================end=====

JPnyc
12-23-2007, 04:29 PM
JavaScript cannot do that. It does not remember from page load to page load.The only way to accomplish this would be with using a cookie, or, adding the value of a variable to the end of the URL. Neither of these is a simple modification.

tinwindow
12-23-2007, 05:37 PM
Thanx there jpnyc,
I wish i knew a little bit more...but without college i have to turn here...
I hope you all have a wonderfull holiday season !!!

JPnyc
12-28-2007, 10:13 AM
No problem. Just for future reference, Java and JavaScript are two entirely different languages with, for the most part, entirely different purposes and capabilities. They do share a very basic similarity in syntax, but that's about all they have in common.

There is a way to do what you want to do with JavaScript, but as I said it's not a simple modification. I can point you in the right direction if you like.

tinwindow
12-28-2007, 06:32 PM
yea...ok...thanx jp...
sure I didn't know that but it surely would be a plus if directed that way....
kewl....thanx

JPnyc
12-28-2007, 07:59 PM
var i;


randomlinks[0]="http://mysite/"
randomlinks[1]="http://mysite/"
randomlinks[2]="http://mysite/"
randomlinks[3]="http://mysite/"
randomlinks[4]="http://mysite/"

function randomlink(){
vali=window.location.href.split('?');
i=vali[1];
if(i==null)
{ i=0; }

window.location.href=randomlinks[i] +"?"+(i++);

}
Try that. It's just off the top of my head, and I haven't tested it.

JPnyc
12-28-2007, 08:18 PM
Actually I take it back, it can't be done without a cookie, which is way more trouble than it could be worth

jjinno
12-29-2007, 07:26 PM
Im lost. At what point did tinwindow say that he wanted to do another page load? So why do we need cookies?

Do I have this correct? You have a webpage with a HUGE list of links... and at the top you have a button that can send the user to a "random" link... but now you want it to be sequential, and also not when the button is clicked anymore, but when the user clicks there mouse anywhere?

I see no problem with doing this, but I would use the power of anchors instead of CSS locations/dimensions.

You might benefit from something like the JQuery (http://jquery.com/) Library... it makes HTML seem much more object-oriented. Take a look anyway, and be prepared to get overwhelmed very fast... but they have a LOT of good tutorials.

JPnyc
01-08-2008, 04:11 PM
At this point: window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]

That changes the page you're on.

tinwindow
01-08-2008, 06:54 PM
kewl

jjinno
01-08-2008, 08:12 PM
OK, so why not do it by comparing the current URL to the list?

IE: On page load, the URL is read, seen to be URL #3, and because of this, URL #4 is used as the "next" button, and URL #2 as the "back" button...

Sounds like a simple pagination problem.

In a file (lets call it 'script.js') put the following code:
function Add(href) {
WebRing[++NumInWebRing] = new WebRingObject(href);
if (location.href.indexOf(href) > -1)
WhereInRing = NumInWebRing;
}

function WebRingObject(href) {
this.href = href;
}

var WhereInRing = 0, NumInWebRing = 0, WebRing = new Array();

Add("index0.html");
Add("index1.html");
Add("index2.html");
Add("index3.html");
Add("index4.html");
Add("index5.html");
Add("index6.html");

if (WhereInRing == 0) WhereInRing = 1;

if (WhereInRing == 1)
PrevInRing = NumInWebRing;
else
PrevInRing = WhereInRing - 1;

if (WhereInRing == NumInWebRing)
NextInRing = 1;
else
NextInRing = WhereInRing + 1;

var output = '';

output += '<A HREF="' + WebRing[PrevInRing].href + '" TARGET="_top">';
output += 'Back<\/A>&nbsp;&nbsp;\|&nbsp;&nbsp;';
output += '<A HREF="' + WebRing[NextInRing].href + '" TARGET="_top">';
output += 'Next<\/A>';

document.write(output);

Then at the bottom of each page, or where you want the link just put the following:

<script type="text/javascript" src="script.js"></script>

Of course, I have **slightly** deviated from your original question which was for ONLY a next button... but I think you can realize that all you have to do is remove the back button lines to get that effect.

Remember, that this code is dumb, so you CAN send it to a page that is not part of your "ring". BUT if you are careful, and only fill the JS array with pages that you KNOW will have your "next page" link... then there is no problem... just continuous looping through pages as per your request.

CODE SITATION: A JavaScript Web-Ring (http://www.irt.org/articles/js085/index.htm)

jjinno
01-08-2008, 08:14 PM
Just cause I never know how clear I am in my explanations...

You need to put the <SCRIPT> code SOMEWHERE on every page that you "Add" to the JavaScript Array. If you DONT, then you wont have a loop, but rather a dead end.

tinwindow
01-09-2008, 05:03 PM
awsome.....
web ring sounds very interesting....
thanx jj

tinwindow
01-19-2008, 09:51 AM
hey jj
the script here does seem to be doing what i wanted and i have had fun with it
thank you a whole bunch...and could you tell me how to incorperate a button instead of the words....?
Thanx

tinwindow
01-20-2008, 06:31 PM
That's ok....
I managed to work in some buttons modified and links connected with them...
Don't know if it is correct but it seems to be functioning perfectly...Thanx to you all for the assistance....
tw

tinwindow
01-20-2008, 06:32 PM
:t That's ok....
I managed to work in some buttons modified and links connected with them...
Don't know if it is correct but it seems to be functioning perfectly...Thanx to you all for the assistance....
tw :t