HighDots Forums  

How can I click on a joke and have a new joke appear?

Javascript JavaScript language (comp.lang.javascript)


Discuss How can I click on a joke and have a new joke appear? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael
 
Posts: n/a

Default How can I click on a joke and have a new joke appear? - 09-22-2003 , 03:46 PM






I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on the
saying and have it load a new one in its place.

Reply With Quote
  #2  
Old   
Lee
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? - 09-22-2003 , 03:56 PM






Michael said:
Quote:
I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on the
saying and have it load a new one in its place.
There are many different possibly ways to display random jokes or sayings.

We would need to know how you're doing it to tell you how best to
change it so that it changes when you click on it.



Reply With Quote
  #3  
Old   
Hywel Jenkins
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? - 09-22-2003 , 04:11 PM



In article <70d75507.0309221246.41561950 (AT) posting (DOT) google.com>, mdh_2972
@hotmail.com says...
Quote:
I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on the
saying and have it load a new one in its place.
<a href="page.htm">blah blah blah</a>

--
Hywel I do not eat quiche
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php


Reply With Quote
  #4  
Old   
Evertjan.
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? - 09-22-2003 , 04:17 PM



Michael wrote on 22 sep 2003 in comp.lang.javascript:

Quote:
I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on the
saying and have it load a new one in its place.

<script>
j=new Array
j[0]="haha"
j[1]="next joke"
j[2]="again"
//....
j[20]="last joke"
</script>

<div onclick="this.innerHTML=j[Math.floor(Math.random()*21)]">
Click here for jokes
</div>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #5  
Old   
Ron
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? - 09-23-2003 , 06:16 PM



"Michael" <mdh_2972 (AT) hotmail (DOT) com> wrote

Quote:
I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on the
saying and have it load a new one in its place.
Javascript is CLIENT SIDE
it can only change what is on the screen based on what has already been sent
to the browser.

If you send a page of say 10 jokes you could display 1 then the next on
clicking a 'next' button.

a suitable technique is to load each on to its own <div> with a unique ID

<div ID='a' style='visibility:hidden;'>
Joke 1
</div>
<div ID='b' style='visibility:hidden;'>

Joke 2
</div>
you would be better setting these all in an identical class controlling
absolute position etc.
then on clicking the button use getElementbyID to unhide the next and hide
the previous

On the other hand,
If you want to retrieve from a server you would have to use the script to
modify the URL and fetch the next page but that as you say is reloading the
page.

HTH

Ron.







Reply With Quote
  #6  
Old   
Michael
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? Part 2 - 09-25-2003 , 03:20 PM



"Evertjan." <exjxw.hannivoort (AT) interxnl (DOT) net> wrote

Quote:
Michael wrote on 22 sep 2003 in comp.lang.javascript:

I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on the
saying and have it load a new one in its place.


script
j=new Array
j[0]="haha"
j[1]="next joke"
j[2]="again"
//....
j[20]="last joke"
/script

div onclick="this.innerHTML=j[Math.floor(Math.random()*21)]"
Click here for jokes
/div

This did not quite work. This is just about what I wanted. I keep
geting a message that says undefined. I think that the part after
innerHTML= has to be in the script too.


Reply With Quote
  #7  
Old   
Evertjan.
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? Part 2 - 09-25-2003 , 03:44 PM



Michael wrote on 25 sep 2003 in comp.lang.javascript:

Quote:
"Evertjan." <exjxw.hannivoort (AT) interxnl (DOT) net> wrote in message
news:<Xns93FEECF282858eejj99 (AT) 194 (DOT) 109.133.29>...
Michael wrote on 22 sep 2003 in comp.lang.javascript:

I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on
the saying and have it load a new one in its place.


script
j=new Array
j[0]="haha"
j[1]="next joke"
j[2]="again"
//....
j[20]="last joke"
/script

div onclick="this.innerHTML=j[Math.floor(Math.random()*21)]"
Click here for jokes
/div


This did not quite work. This is just about what I wanted. I keep
geting a message that says undefined. I think that the part after
innerHTML= has to be in the script too.

It works all right, tested on IE6, but you have to fill in all 20 texts,
otherwise you get that "undefined"

Or you could do Math.random()*3

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #8  
Old   
Douglas Crockford
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? Part 2 - 09-25-2003 , 04:12 PM



Quote:
I know how to display random jokes or sayings. But only if I reload
the page does the script select a new saying. How can I click on
the saying and have it load a new one in its place.


script
j=new Array
j[0]="haha"
j[1]="next joke"
j[2]="again"
//....
j[20]="last joke"
/script

div onclick="this.innerHTML=j[Math.floor(Math.random()*21)]"
Click here for jokes
/div


This did not quite work. This is just about what I wanted. I keep
geting a message that says undefined. I think that the part after
innerHTML= has to be in the script too.


It works all right, tested on IE6, but you have to fill in all 20 texts,
otherwise you get that "undefined"

Or you could do Math.random()*3
Or better, use Math.floor(Math.random() * j.length). That way, you don't have to
edit the script when the number of jokes changes. And better still, use the
literal array notation. That way, you don't have to number the jokes.

j = [
"haha",
"again",
"last joke"];

http://www.JSON.org



Reply With Quote
  #9  
Old   
Mike Painter
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? Part 2 - 09-25-2003 , 10:12 PM




"Douglas Crockford" <nospam (AT) laserlink (DOT) net> wrote

<snip >
Quote:
It works all right, tested on IE6, but you have to fill in all 20 texts,
otherwise you get that "undefined"

Or you could do Math.random()*3

Or better, use Math.floor(Math.random() * j.length). That way, you don't
have to
edit the script when the number of jokes changes. And better still, use
the
literal array notation. That way, you don't have to number the jokes.

j = [
"haha",
"again",
"last joke"];

Or realize that there are no new jokes.




Reply With Quote
  #10  
Old   
Evertjan.
 
Posts: n/a

Default Re: How can I click on a joke and have a new joke appear? Part 2 - 09-26-2003 , 04:00 AM



Douglas Crockford wrote on 25 sep 2003 in comp.lang.javascript:

Quote:
j=new Array
j[0]="haha"
j[1]="next joke"
j[2]="again"
//....
j[20]="last joke"
/script

Or you could do Math.random()*3

Or better, use Math.floor(Math.random() * j.length). That way, you
don't have to

Not in the case under investigation, where there is a gap in the array !



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.