Help: JavaScript Code. Important!!!

Status
Not open for further replies.

abhi.eternal

In search of light...
hi. i need your help for developing a webpage.

i have created a table with 3 columns. now i want to load images (300*250) in the middle column from a particular folder. but they should be selected randomly. after an image is selected, by clicking it a new link (target: blank) should open.

i think this can be achieved through JScript. If there are 20 images in a folder, here is what I think should be the code structure:

1: Generate a random number between 1-20. say x.
2: If x=1, then <a href="link1" target="_blank"><img src="image1.gif" alt="image1" width="300" height="250" border="0" /></a>
If x=2, then <a href="link2" target="_blank"><img src="image2.gif" alt="image2" width="300" height="250" border="0" /></a>...
3: If x < 1 and X > 20 goto step1.

this is just the structure. please provide me the exact JScript codes to do what I intend and its implementation.

please help. i promise the sender of the first solution thats works will have a nice small surprise for him/her.

thank you.
 

mediator

Technomancer
Since ur having a small surprise only for 1st part so here it is!

<script>
function get_random()
{
var ranNum= Math.floor(Math.random()*20);
return ranNum;
}
</script>
This Generates numbers 0-19. U can ofcors just increment the variable to make it 1-20.

And please don't add such "bargain" like statements like "small surprise" next time! :)
 

ahref

In the zone
@mediator actually he want complete solution and not solution of generating random number only.

I don't think there is anything wrong, if he promise some gift for solution.
 
OP
abhi.eternal

abhi.eternal

In search of light...
Full solutions only please.

mediator said:
And please don't add such "bargain" like statements like "small surprise" next time!
This is not a bargain. U don't know how much IMPORTANT this is for me!
 

mediator

Technomancer
^^I understand. I know its important to u. Thats why u cud have just said so.

Neways I hope this is wat u r looking for!
<html>
<script>
function get_random()
{
var ranNum= Math.floor(Math.random()*20);
ranNum+=1;
return ranNum;
}
var x=get_random();
if (x==1) { window.open('*www.yahoo.com')}
else if(x==2) { document.write('<a href="link2" target="_blank"><img src="image2.gif" alt="image2" width="300" height="250" border="0" /></a>')}
else document.write('Mediator is cool');

</script>
</html>
Observe the if-else statements and modify them as u wish! :)
 
OP
abhi.eternal

abhi.eternal

In search of light...
thanx. i'll try it out.

but i have a few questions:

{
var ranNum= Math.floor(Math.random()*20);
ranNum+=1;
return ranNum;
}

will these generate no.s between on 1-20?

please define these functions:

window.open
document.write

else document.write('Mediator is cool');

no offense, but need that else it should again goto to the step to generate random numbers 1-20.

thanx for your prompt action.
 

mediator

Technomancer
will these generate no.s between on 1-20?
Yes, and only bet. that range!



1. window.open
2. document.write
1. to a open a new window with the given link
2. To write anything on the html page



no offense, but need that else it should again goto to the step to generate random numbers 1-20.
3: If x < 1 and X > 20 goto step1.
Such case (if entry) won't arise, so there is no need to goto step1.



else document.write('Mediator is cool');
This code shud not be removed without orginal developer's consent! :D
 

piyush gupta

Cyborg Agent
hey abhi i feel u need to understand basics of Javascript first before asking for some code

u asking for document.write and window.open
 
OP
abhi.eternal

abhi.eternal

In search of light...
piyush619 said:
hey abhi i feel u need to understand basics of Javascript first before asking for some code

u asking for document.write and window.open
i dont have time to brush up JScript, thats why i asked for the codes.
 

tuxfan

Technomancer
Just one suggestions.

Instead of if .. else .. elseif, you should use switch case.
Code:
switch (x)
{
  case 1:
    [I]statement 1;
    statement 2;[/I]
    break;
  case 2:
[I]    statement 1;
    statement 2;[/I]
    break;
  case 3:
[I]    statement 1;
    statement 2;[/I]
     break;
}
But if you just want to open an image based on the number and not do anything else, you don't even need a switch case!! You can do this.

Code:
  document.write("<a href='imgfull" + x + ".jpg' target=_blank><img src='img" + x + ".jpg'></a>");
(This code may have just some small syntax error of quotes which will be very easy to correct once you test it).

Any gift for optimised code? :))
 
Status
Not open for further replies.
Top Bottom