Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How can I click on a joke and have a new joke appear?

Reply
Thread Tools

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

 
 
Michael
Guest
Posts: n/a
 
      09-22-2003
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
 
 
 
 
Lee
Guest
Posts: n/a
 
      09-22-2003
Michael said:
>
>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
 
 
 
 
Hywel Jenkins
Guest
Posts: n/a
 
      09-22-2003
In article < >, mdh_2972
@hotmail.com says...
> 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
 
Evertjan.
Guest
Posts: n/a
 
      09-22-2003
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>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Ron
Guest
Posts: n/a
 
      09-23-2003
"Michael" <> wrote in message
news: om...
> 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
 
Michael
Guest
Posts: n/a
 
      09-25-2003
"Evertjan." <> wrote in message news:<Xns93FEECF282858eejj99@194.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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      09-25-2003
Michael wrote on 25 sep 2003 in comp.lang.javascript:

> "Evertjan." <> wrote in message
> news:<Xns93FEECF282858eejj99@194.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
 
Douglas Crockford
Guest
Posts: n/a
 
      09-25-2003
> >> > 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
 
Mike Painter
Guest
Posts: n/a
 
      09-26-2003

"Douglas Crockford" <> wrote in message
news:bkvlq1$jas$...
<snip >
> > 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
 
Evertjan.
Guest
Posts: n/a
 
      09-26-2003
Douglas Crockford wrote on 25 sep 2003 in comp.lang.javascript:

>> >> 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

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
click click click Andy Morrison Computer Support 7 12-20-2007 06:50 AM
How to fire both event button click and textchanged when button is click and text is changed Amy ASP .Net 0 06-01-2006 02:33 PM
joke-dirty joke Joyce Danielson Computer Support 0 02-08-2005 11:42 PM
click() function does not have same effect as real click (IE 6) Lazlo Woodbine Javascript 1 05-14-2004 08:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57