473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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.
Jul 20 '05 #1
16 2196
In article <70**************************@posting.google.com >, 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
Jul 20 '05 #2
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)
Jul 20 '05 #3
Lee
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.

Jul 20 '05 #4
Ron
"Michael" <md******@hotmail.com> wrote in message
news:70**************************@posting.google.c 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.

Jul 20 '05 #5
"Evertjan." <ex**************@interxnl.net> wrote in message news:<Xn********************@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.
Jul 20 '05 #6
Michael wrote on 25 sep 2003 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
news:<Xn********************@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)
Jul 20 '05 #7
> >> > 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

Jul 20 '05 #8

"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bk**********@sun-news.laserlink.net...
<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.
Jul 20 '05 #9
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)
Jul 20 '05 #10
Mike Painter wrote on 26 sep 2003 in comp.lang.javascript:
j = [
"haha",
"again",
"last joke"];

Or realize that there are no new jokes.


That is a good one !

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #11
"Mike Painter" <md*********@att.net> writes:
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bk**********@sun-news.laserlink.net...
<snip >
That way, you don't have to number the jokes.
Or realize that there are no new jokes.


#751 ?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12
> >> >> 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 !


The point was that if the literal object notation is used, there won't be a gap.

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

Smaller, faster, easier to edit, no gaps.

http://www.JSON.org

Jul 20 '05 #13
"Evertjan." <ex**************@interxnl.net> wrote in message news:<Xn********************@194.109.133.29>...
Michael wrote on 25 sep 2003 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
news:<Xn********************@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


Sorry. But for some reason, I could not quite get it to work. So I
messed around until I got it to work. Here is what I came up with.
<script>
j=new Array(2)
index = Math.floor(Math.random() * j.length) + 1;
</script>

<div onclick="this.innerHTML=index">Click Here</div>
Now I have another question. I just wanted to take it a step at a
time.

What I really would like to do, is to put each joke into seperate .js
files. Otherwise, since I have hundreds of them it would bog down the
page with too much data and take too long to load into the browser. I
read at www.netmechanic.com where if it takes more than 8 seconds to
load the page then most people will just get bored and go elsewhere.

I have lots of random content and I do not like it when some one has
to reload the whole page to see a different joke or whatever because
it increments the counter. So I thought it would be more convenient if
the person could just click on the joke TEXT and have it display a new
joke each time.

But not just 1 time. Over and over again until they got tired of
reading jokes.

I tried it with an iframe and then just put onclick=location.reload()
in the body tag of the html file in the iframe. That way each time I
clicked on the joke itself the iframe subpage reloaded and displayed a
new joke from the array. It worked great, except that some of the
jokes were larger than the iframe window and then I could not see the
whole thing. I placed each joke in a table, and then gave it a height
and width. But I did not know how to transfer the height and with of
the table in the iframe to the iframe height and width. You know so
that if the table with the joke became larger or smaller the iframe
would resize as needed.

So anyway, I thought there must be an easier way.
Jul 20 '05 #14
"Evertjan." <ex**************@interxnl.net> wrote in message news:<Xn********************@194.109.133.29>...
Michael wrote on 25 sep 2003 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
news:<Xn********************@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


That script example I last gave you is no good. It just displays the
index number, not the joke in the array. For some reason I cannot get
it to display the joke. If I put this.innerHTML=j[1] it still says
undefined.
Jul 20 '05 #15
Michael wrote on 26 sep 2003 in comp.lang.javascript:
That script example I last gave you is no good. It just displays the
index number, not the joke in the array. For some reason I cannot get
it to display the joke. If I put this.innerHTML=j[1] it still says
undefined.


What script are you talking about ?
Show it again please.

1 are you using IE and what version? or something else ?

2 start debugging:

this.innerHTML="Yes"
alert(j[1])

etc.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #16

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:3c**********@hotpop.com...
"Mike Painter" <md*********@att.net> writes:
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bk**********@sun-news.laserlink.net...
<snip >
That way, you don't have to number the jokes.

Or realize that there are no new jokes.


#751 ?


Odd, I don't hear anybody laughing.
Some people just can't tell a joke.
Jul 20 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Hubbard | last post by:
By one-click deployment I mean the ability to click on an EXE located on a server (internet or intranet) and run it locally. Due to the default restrictions placed on such code (no file...
68
by: Ted Nicols | last post by:
That's what I keep asking myself whenever develop in .NET. Is this a joke a farse or just a bad dream? ..NET is slow, actually slow is just a polite word I can use in a newsgroup. ..NET is...
4
by: Matt Horsey | last post by:
There are two men, an Englishmen and an Australian, wearing their respective cricket uniforms, in the men's room. After finishing up, the English man walks to the sink to wash up. The Aussie man...
2
by: Sean | last post by:
Hi, I have a treeview and user can right click the treenodes, depending on the nodes, different shortcut menu will appear. I want only the shortcut menu to appear if the point of the right...
23
by: Skybuck Flying | last post by:
Some coders don't need an obfuscator, they are the obfuscator. Bye, Skybuck =D
2
by: mindscreen | last post by:
I want to use Right-Click menu of mouse in any form or anywhere in my program? How can I able to do this? I want that when user right-click on any button or form a special pop-up menu will appear...
2
by: Ed Dror | last post by:
Hi there, Based on Microsoft ASP.NET SDK treeview control binding to northwind database (Categoried, Products) I added the following code Protected Sub TreeView1_SelectedNodeChanged(ByVal...
6
by: charvi | last post by:
hi is there any ascii code for mouse click this is because i have atext box and a list box.when i enter two letters in the text box all the related names will appear in the list box i want to...
8
by: starrysky | last post by:
I have a program which puts an icon in the notification area and has a menu associated with it available by right clicking on the icon. I want the menu items to be selected by single left clicks but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.