Passing a value to event handler

S

Steve

The following javascript file works:

window.onload = writeMessage;
function writeMessage() {
document.getElementById("helloMessage").innerHTML = "bottom";
}

But when I pass a value to the event handler like this:

window.onload = writeMessage("bottom");
function writeMessage(text) {
document.getElementById("helloMessage").innerHTML = text;
}

I get the error "document.getElementById("helloMessage") has no
properties"

How can I pass a value to the event handler?
 
V

VK

But when I pass a value to the event handler like this:

window.onload = writeMessage("bottom");
function writeMessage(text) {
document.getElementById("helloMessage").innerHTML = text;

}

I get the error "document.getElementById("helloMessage") has no
properties"

How can I pass a value to the event handler?

window.onload = writeMessage("bottom"); means:
"- Execute writeMessage() function with argument "bottom" and assign
the return value as a function reference to window.onload handler."

To do you what you really want you may go two ways: 1) by the ancient
one and 2) over augmentation.

1)
<html>
....
<body onload="writeMessage('bottom')">
....

2)
....
function writeMessage() {
text = arguments.callee.args['text'] || "No hello message";
document.getElementById("helloMessage").innerHTML = text;
}

writeMessage.args = {"text":"hello", "foo":"bar"};
window.onload = writeMessage;
 
S

Steve

Many thanks VK. I have decided to use the ancient method :)

What are the pro's and con's of having the onload in the html file
versus having it in the javascript?

Cheers,

But when I pass a value to the event handler like this:
window.onload = writeMessage("bottom");
function writeMessage(text) {
document.getElementById("helloMessage").innerHTML = text;

I get the error "document.getElementById("helloMessage") has no
properties"
How can I pass a value to the event handler?

window.onload = writeMessage("bottom"); means:
"- Execute writeMessage() function with argument "bottom" and assign
the return value as a function reference to window.onload handler."

To do you what you really want you may go two ways: 1) by the ancient
one and 2) over augmentation.

1)
<html>
...
<body onload="writeMessage('bottom')">
...

2)
...
function writeMessage() {
text = arguments.callee.args['text'] || "No hello message";
document.getElementById("helloMessage").innerHTML = text;

}

writeMessage.args = {"text":"hello", "foo":"bar"};
window.onload = writeMessage;
 
V

VK

What are the pro's and con's of having the onload in the html file
versus having it in the javascript?

By using the original way over <body> arguments you are decreasing
your code flexibility, because your JavaScript and HTML are getting
tied up. It means that in order to change the script (say to call
another function onload or to call it with another argument) each time
you have to change your HTML as well. If this is a solution for
yourself and if it doesn't matter to you - then it doesn't matter at
all :)

Also in the context of the modern "content / presentation /
scripting / everything / separation" such code may look irritating for
some evangelistic eyes - so leading to narrations about "separation
rules of the modern Web" etc. If it doesn't matter to you - then it
doesn't matter at all, just keep some firm but polite "go to hell"
answers for such cases :)
 
R

RobG

But when I pass a value to the event handler like this:
window.onload = writeMessage("bottom");
function writeMessage(text) {
document.getElementById("helloMessage").innerHTML = text;

I get the error "document.getElementById("helloMessage") has no
properties"
How can I pass a value to the event handler?

window.onload = writeMessage("bottom"); means:
"- Execute writeMessage() function with argument "bottom" and assign
the return value as a function reference to window.onload handler."

To do you what you really want you may go two ways: 1) by the ancient
one and 2) over augmentation.

1)
<html>
...
<body onload="writeMessage('bottom')">
...

2)
...
function writeMessage() {
text = arguments.callee.args['text'] || "No hello message";
document.getElementById("helloMessage").innerHTML = text;

}

writeMessage.args = {"text":"hello", "foo":"bar"};
window.onload = writeMessage;

Method 2 seems somewhat convoluted. The list is a bit short:

3)

window.onload = function(){
writeMessage(arg1, arg2, ...);
}

4) Various method using attachEvent/addEventListener:

<URL:
http://groups.google.com.au/group/c...attachEvent+addeventlistener#93561d5a2ebdb4aa
 
V

VK

Method 2 seems somewhat convoluted. The list is a bit short:

3)

window.onload = function(){
writeMessage(arg1, arg2, ...);

}

Yeah, that would be much simpler for the situation in question. To
keep the numbering fair :) - that one above should be the number 2,
not 3, because it is internally the equivalent of 1) <body
onload="writeMessage(arg1,...)">
 
B

Bruno Desthuilliers

Steve a écrit :
The following javascript file works:

window.onload = writeMessage;
function writeMessage() {
document.getElementById("helloMessage").innerHTML = "bottom";
}

But when I pass a value to the event handler like this:

window.onload = writeMessage("bottom");
function writeMessage(text) {
document.getElementById("helloMessage").innerHTML = text;
}

I get the error "document.getElementById("helloMessage") has no
properties"

How can I pass a value to the event handler?

window.onload = function() {
writeMessage("bottom");
};
 
T

Thomas 'PointedEars' Lahn

Steve said:
Many thanks VK. I have decided to use the ancient method :)

The approach works since the first days of DOM scripting, but it hardly
qualifies as "ancient".
What are the pro's and con's of having the onload in the html file
versus having it in the javascript?

The former is standardized (since HTML 3.2) and (therefore) interoperable,
the latter is not.

I'd strongly advise you not to listen to VK's fairytales, and to read
previous threads about DOM scripting instead.

Please don't top-post, reply below trimmed quotes (as explained and
recommended in the FAQ http://jibbering.com/faq/).


PointedEars
 
D

Darko

The approach works since the first days of DOM scripting, but it hardly
qualifies as "ancient".


The former is standardized (since HTML 3.2) and (therefore) interoperable,
the latter is not.

What's non-standard with:

window.onload = function()
{
// ...
};

?
 
R

RobG

Steve wrote: [...]
What are the pro's and con's of having the onload in the html file
versus having it in the javascript?
The former is standardized (since HTML 3.2) and (therefore) interoperable,
the latter is not.

What's non-standard with:

window.onload = function()

The onload attribute of the body element has been included in the W3C
HTML recommendation since version 3.2 - hence Thomas' assertion that
it is *standarised*.

The onload property of the window object is part of DOM 0, which is
not a standard, but could be said to be just as standardised as the
HTML body element's onload attribute referred to above.

I think it is a matter of opinion as to whether one is more
standardised than the other. If real standards are considred, the
discussion would have to turn to ISO standard HTML[1], where the DTD
doesn't include script elements or attributes for intrinisic events
(i.e. there is no onload attribute, and no way of specifying a code
that might do something with it), so consideration of actual standards
renders the entire conversation moot.

1. <URL: https://www.cs.tcd.ie/15445/15445.html >
 
T

Thomas 'PointedEars' Lahn

RobG said:
Steve wrote: [...]
What are the pro's and con's of having the onload in the html file
versus having it in the javascript?
The former is standardized (since HTML 3.2) and (therefore)
interoperable, the latter is not.
What's non-standard with:

window.onload = function()

The onload attribute of the body element has been included in the W3C
HTML recommendation since version 3.2 - hence Thomas' assertion that it
is *standarised*.
Exactly.

The onload property of the window object is part of DOM 0, which is not a
standard, but could be said to be just as standardised as the HTML body
element's onload attribute referred to above.

Wishful thinking.
I think it is a matter of opinion as to whether one is more standardised
than the other.

It isn't. One is specified in actually several Web standard documents, the
other one is not.
If real standards are considred,

ISO standards are no more "real standards" than W3C standards.
the discussion would have to turn to ISO standard HTML[1], where the DTD
doesn't include script elements or attributes for intrinisic events (i.e.
there is no onload attribute, and no way of specifying a code that might
do something with it),

Which is one aspect of why it is mostly useless nowadays.
so consideration of actual standards renders the entire conversation
moot.

Non sequitur.

Whether you like it or not, the W3C Recommendations, especially the HTML 3.2
and 4.01 Specifications, *are* industrial Web standards.

http://www.rfc-editor.org/rfc/rfc2854.txt
http://www.w3.org/Consortium/


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
Darko said the following on 11/8/2007 8:50 PM:

Please don't get him started. He is going to tell you that the "window"
object is proprietary and then it just goes on from there.

It *is* still a proprietary, host-defined property of the Global Object,
hence all of its properties are proprietary and host-defined.

There are efforts underway to standardize the interface, however that draft
still lacks commonly used features: http://www.w3.org/TR/Window/ Until that
is resolved and implemented, there is no standard for that interface. Period.

And if you would have applied common sense, you would have recognized the
reported inability of your own mobile device's user agent to execute
window.alert() as a sure sign that the object and its implementations are
not a standard at all.


PointedEars
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top