How come this doesn't work ?

J

Jack Jones

<head>
<script>
function say(message) {
var text;
document.body.onclick = function () {alert(text);};
}
</script>
</head>
<body onload="say('test')">
This is a <i>test</i> document.
</body>


I was trying to write an example that would pring an alert box when the
page is displayed and then an alert box every time the document is
clicked, thus proving that var text is always in scope. But sadly
nothing happens instead.
 
E

Erwin Moller

Jack said:
<head>
<script>
function say(message) {
var text;
document.body.onclick = function () {alert(text);};
}
</script>
</head>
<body onload="say('test')">
This is a <i>test</i> document.
</body>


I was trying to write an example that would pring an alert box when the
page is displayed and then an alert box every time the document is
clicked, thus proving that var text is always in scope. But sadly
nothing happens instead.

Well,

If you click on the text 'This is a test document.' it does work.
You body just ends there.
So the onclick is added as you expected, but the body of your document is
shorter than you expected. ;-)

Regards,
Erwin Moller
 
J

Jack Jones

* Erwin Moller said:
Well,

If you click on the text 'This is a test document.' it does work.
You body just ends there.
So the onclick is added as you expected, but the body of your document is
shorter than you expected. ;-)

ahhhhhhhh..... now I don't really feel like a clown shoes!

Thanks!
 
J

Jack Jones

Actually I get...... "undefined" when I click on the body and , I don't
get an alert box when the page loads. So there must be something else
wrong too. So many mistakes in code so short? eek
 
R

Robin

Jack said:
Actually I get...... "undefined" when I click on the body and , I don't
get an alert box when the page loads. So there must be something else
wrong too. So many mistakes in code so short? eek

your function takes an argument 'message' but uses a local variable
'text' that doesn't get set.
 
T

Tom Cole

Actually I get...... "undefined" when I click on the body and , I don't
get an alert box when the page loads. So there must be something else
wrong too. So many mistakes in code so short? eek

You won't get an alert box when the page loads because your alert call
is inside an event handler, onclick. So .... until you click, no
alert...

You get undefined because you've passed the message value in with a
parameter name "message" (see function say(message)..) but you never
use it or assign it to anything else. Instead you elect to "alert" a
variable named "text" which you have never initialized.

HTH.
 
J

Jack Jones

* Robin said:
your function takes an argument 'message' but uses a local variable
'text' that doesn't get set.
oops thats a type. should be

var text = message; in variable declaration. my bad. ta :)
 
J

Jack Jones

* Tom Cole said:
You won't get an alert box when the page loads because your alert call
is inside an event handler, onclick. So .... until you click, no
alert...

No its ALSO called in onload

<body onload="say('test')">

So how come THAT doesn't work?
 
W

Walton

No its ALSO called in onload

<body onload="say('test')">

So how come THAT doesn't work?

probably because you're still not clicking on the body.

<html>
<head>
<script type="text/javascript">
function say(message) {
var text = message;
document.onclick = function () {alert(text);};
}
</script>
</head>
<body onload="say('test')">
This is a <i>test</i> document.
</body>
</html>

i put the gave the document the onclick event handler so you can click
wherever you please on the page now and get the alert message.

you have to *click* the page if you want to see the alert message. if
you want the alert to popup when the page loads do this:

<body onload="alert('test')">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top