473,406 Members | 2,217 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,406 software developers and data experts.

How come this doesn't work ?

<head>
<script>
function say(message) {
var text;
document.body.onclick = function () {alert(text);};
}
</script>
</head>
<body onload="say('test')">
This is a <i>test</idocument.
</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.
Mar 16 '07 #1
8 2332
Jack Jones wrote:
<head>
<script>
function say(message) {
var text;
document.body.onclick = function () {alert(text);};
}
</script>
</head>
<body onload="say('test')">
This is a <i>test</idocument.
</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

Mar 16 '07 #2
* Erwin Moller wrote:
Jack Jones wrote:
><head>
<script>
function say(message) {
var text;
document.body.onclick = function () {alert(text);};
}
</script>
</head>
<body onload="say('test')">
This is a <i>test</idocument.
</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. ;-)
ahhhhhhhh..... now I don't really feel like a clown shoes!

Thanks!
Mar 16 '07 #3

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
Mar 16 '07 #4
Jack Jones wrote:
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.
Mar 16 '07 #5
On Mar 16, 6:22 am, Jack Jones <jack.jo...@zxcv231.co.ukwrote:
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.

Mar 16 '07 #6
* Robin wrote:
Jack Jones wrote:
>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.
oops thats a type. should be

var text = message; in variable declaration. my bad. ta :)
Mar 16 '07 #7
* Tom Cole wrote:
On Mar 16, 6:22 am, Jack Jones <jack.jo...@zxcv231.co.ukwrote:
>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...
No its ALSO called in onload

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

So how come THAT doesn't work?
Mar 16 '07 #8
On Mar 16, 8:36 am, Jack Jones <jack.jo...@zxcv231.co.ukwrote:
* Tom Cole wrote:
On Mar 16, 6:22 am, Jack Jones <jack.jo...@zxcv231.co.ukwrote:
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...

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</idocument.
</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')">

Mar 16 '07 #9

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

Similar topics

4
by: Evan Dekker | last post by:
hi all im not why this code doesn't work: ///////// import java.applet.*; import java.awt.*; public class TestApplet extends Applet { public void init() {
15
by: AV | last post by:
Hallo any idea why the following code doesn't work? ////////////////////////////////// function myfunc(){ with(this){ prop="hallo world"; } }
4
by: Iver Erling Årva | last post by:
I have an application that uses a window.open() to open it's own main window where all my programs takes place. I use a timeout so if nothing goes on for 15 minutes the document below is called. To...
3
by: webdev | last post by:
Hi, Code below dynamically adds an input box to a form, creating a new name attribute as it goes... (many thanks to Martin Honnen for getting me this far ;0) Problem is - in IE5.5 only the first...
2
by: KK | last post by:
Hi, I want my users to be able to enter date in any format. Such as dd/mm/yyyy, mm/dd/yyyy etc... then I use the following method to see wether this is a valid date : try {
4
by: Bob | last post by:
Hi, this works with IE 7 (you can see the red label with 60px length), but not with Netscape 8 (nothing appears): <form id="form1" runat="server"> <asp:Label ID="Label2" runat="server"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.