Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > event handlers

Reply
Thread Tools

event handlers

 
 
josh
Guest
Posts: n/a
 
      08-14-2007
Hi, I read that professional web developer write javascript event
handler out of html tag attributes
to separate script from html structure...like css....

so when I must write an handler I should do:

i.e. document.body.onclick=foo
or document.body.addEventListener("click", foo, false);

but if I had to pass parameters????

i.e.

<script>
function foo(txt)
{
alert(txt)
}

</script>
<body onclick="foo('clicked me!!!!')"

Thanks

P.S. anonymous function doesn't seem resolve it because I can only
write arguments....Am I in error??

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 9:03 pm, josh <(E-Mail Removed)> wrote:
> Hi, I read that professional web developer write javascript event
> handler out of html tag attributes
> to separate script from html structure...like css....


That is one strategy that can be employed, and it is a good strategy,
however there are a number of ways to achieve it if you consider both
server and client based approaches.


> so when I must write an handler I should do:
>
> i.e. document.body.onclick=foo


If you want to add a single handler, and (usually) wait for the onload
event to add it, that is one way. Or you can use a server framework
that adds the appropriate HTML attribute:

<body onclick="foo();" ... >

and have the onload handler available as soon as the body element is
(and download fewer bytes to as a bonus).


> or document.body.addEventListener("click", foo, false);


And not support IE? I think you'd need a handler-adding function that
accommodates at least addEventListener and attachEvent, with fallback
to a hand-crafted method that can add multiple event handlers.


> but if I had to pass parameters????
>
> i.e.


Or do you mean e.g.?


> <script>
> function foo(txt)
> {
> alert(txt)
>
> }
>
> </script>
> <body onclick="foo('clicked me!!!!')"


window.onload = function() {
document.body.onclick = function(){foo('click me!!!!');
}

However, as noted above, if you use a server framework that adds the
handler in the HTML, you actually maintain the separation of script
and HTML at the server where it counts for you, download less code and
your visitors aren't waiting for the onload event to add your handler.


> P.S. anonymous function doesn't seem resolve it because I can only
> write arguments....Am I in error??


I don't know what you mean by "I can only write arguments".


--
Rob

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-14-2007
josh wrote:
> Hi, I read that professional web developer write javascript event
> handler out of html tag attributes to separate script from html structure


Since DOM stripting is inherently attached to the markup it operates on,
either you have read utter nonsense or you have completely misunderstood
what was written. If the former and it was a book, rip it apart, shredder
it, throw it in the trash can, burn it, and sue the author for being
incompetent.

> ..like css....


CSS is a formatting language (not a programming language) which is attached
to its markup with one or more of element relation, `id', `class' or `style'
attribute.

> so when I must write an handler I should do:
>
> i.e. document.body.onclick=foo
> or document.body.addEventListener("click", foo, false);


.... or document.body.attachEvent("onclick", ...) (MSHTML) or whatever
proprietary means the UA's DOM supports.

And you would have to test for each of it, with a remaining margin of error
since not all DOM features can be tested for exactly. Whereas with an
entirely standards compliant event handler attribute you would not have to.

Now which approach would the professional Web developer prefer?

> but if I had to pass parameters????


Your Question Mark key is borken. But if you had to pass parameters, you
would use a function expression:

document.body.addEventListener("click", function() { foo(bar); }, false);

> i.e.
>
> <script>


http://validator.w3.org/

> function foo(txt)
> {
> alert(txt)
> }
>
> </script>
> <body onclick="foo('clicked me!!!!')"


That is the way, with only one exclamation mark of course.

> [...]
> P.S. anonymous function doesn't seem resolve it because I can only
> write arguments....Am I in error??


Parse error.


PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      08-14-2007
On 14 Ago, 13:46, RobG <(E-Mail Removed)> wrote:
> On Aug 14, 9:03 pm, josh <(E-Mail Removed)> wrote:


> Or do you mean e.g.?

yes I mean it!

> I don't know what you mean by "I can only write arguments".


Oh, yes I used that keyword improperly. I wanted to say:
e.g.
document.body.onclick = function(arg1)
{
// ...
}
but in that code formal parameter arg1 is not usable because I can't
pass any actual parameter

Josh

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-14-2007
josh wrote:
> On 14 Ago, 13:46, RobG <(E-Mail Removed)> wrote:
>> I don't know what you mean by "I can only write arguments".

>
> Oh, yes I used that keyword improperly. I wanted to say:
> e.g.
> document.body.onclick = function(arg1)
> {
> // ...
> }
> but in that code formal parameter arg1 is not usable because I can't
> pass any actual parameter


Event listeners have a fixed signature that is defined by the UA's DOM.
See my other followup.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Stevo
Guest
Posts: n/a
 
      08-14-2007
Thomas 'PointedEars' Lahn wrote:
> either you have read utter nonsense or you have completely misunderstood
> what was written. If the former and it was a book, rip it apart, shredder
> it, throw it in the trash can, burn it, and sue the author for being
> incompetent.
>
> Now which approach would the professional Web developer prefer?
>
>> but if I had to pass parameters????

>
> Your Question Mark key is borken.


Did anyone ever tell you that your messages come across as being very
nasty. I wonder if you're that evil to people you work with that ask you
questions face to face, or if it's just here where you don't have to
face the people you attack. Don't bother replying to me, I'm killing
this thread so Thunderbird doesn't show it to me anymore. I don't want
to see what kind of response you'll give to me. I already imagine it
will be savage so I don't need to see it.

You need to remember one thing when you're answering a question here. If
they knew as much as you do, they wouldn't come asking questions. Not
everyone has your skill level in Javascript or the myriad of web
technology that's available. People get thrown in the deep end sometimes
by their boss, or they're a kid that wants to teach themself, or maybe
they just inherited a support task. You might say to them well go learn
it then, and that's just what they're doing. No javascript book explains
every type of programming task you'll encounter, so when a newbie has
questions that the book (or google) don't provide an answer to, then
this is one of the places they'll find their way to. Calling them stupid
and shouting the rules of a group they've never visited is just plain
nasty, and you're the worst offender of this.
 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      08-14-2007
On 14 Ago, 13:48, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:

> Since DOM stripting is inherently attached to the markup it operates on,
> either you have read utter nonsense or you have completely misunderstood
> what was written. If the former and it was a book, rip it apart, shredder
> it, throw it in the trash can, burn it, and sue the author for being
> incompetent.


I quote the exact phrase taken by JavaScript Bible Sixth Ed and I
think that
this is a great book and the author Danny is also a professional
javascript programmer!
"""" Separating content from scripting
Those who use CSS to style their sites have learned that separating
style definitions from the HTML markup
makes a huge improvement in productivity when it comes time to change
colors or font specifications
throughout a site. Instead of having to modify hundreds of <font> tag
specifications scattered around the
site, all it takes is a simple tweak of a single CSS rule in one .css
file to have that change be implemented
immediately across the board.
The notion of using HTML purely for a page's structure has also
impacted scripting. It is rare these days for
a professional scripter to put an event handler attribute inside an
HTML tag. That would be considered too
much mixing of content with behavior. In other words, the HTML markup
should be able to stand on its
own so that those with nonscriptable browsers (including those with
vision or motor disabilities who use
specialized browsers) can still get the fundamental information
provided by the page. Any scripting that
impacts the display or behavior of the page is added to the page after
the HTML markup has loaded and
rendered. Even assigning events to elements is done by script after
the page load.
Script code is more commonly linked into a page from an external .js
file. This isn't part of the separation
of content and scripts trend, but a practice that offers many
benefits, such as the same code being instantly
usable on multiple pages. Additionally, when projects involve many
code chefs, scripters can work on their
code while writers work on the HTML and designers work on their
external CSS code """""

However that is only an opinion ... and I'm agree with him (may be you
not)

> CSS is a formatting language (not a programming language) which is attached
> to its markup with one or more of element relation, `id', `class' or `style'
> attribute.


in fact also CSS is attached to HTML (like javascript object code to
tags) and
today web developer doesn't "mix" presentation code (css) with
structural code (html)...
you are contradicting yourself.....

> document.body.addEventListener("click", function() { foo(bar); }, false);


thanks this is the answer I was searching.....

Josh


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-14-2007
josh wrote:
> [...] Thomas 'PointedEars' Lahn [...] wrote:
>> Since DOM stripting is inherently attached to the markup it operates on,
>> either you have read utter nonsense or you have completely misunderstood
>> what was written. If the former and it was a book, rip it apart, shredder
>> it, throw it in the trash can, burn it, and sue the author for being
>> incompetent.

>
> I quote the exact phrase taken by JavaScript Bible Sixth Ed and I
> think that this is a great book and the author Danny is also a professional
> javascript programmer!


You are wrong. The book you are referring to is merely considered by some
to be the best of the bad JavaScript books known around here.

I for one have seen too many very bad examples out of it being posted here
to refrain from ever buying it and to recommend against buying it.
Partially because it is hopelessly outdated, partially because it is
factually wrong. YMMV, but then please don't complain here if it does not
work as expected. You have been warned.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-14-2007
Thomas 'PointedEars' Lahn wrote:
> josh wrote:
>> [...] Thomas 'PointedEars' Lahn [...] wrote:
>>> Since DOM stripting is inherently attached to the markup it operates on,
>>> either you have read utter nonsense or you have completely misunderstood
>>> what was written. If the former and it was a book, rip it apart, shredder
>>> it, throw it in the trash can, burn it, and sue the author for being
>>> incompetent.

>> I quote the exact phrase taken by JavaScript Bible Sixth Ed and I
>> think that this is a great book and the author Danny is also a professional
>> javascript programmer!

>
> You are wrong. The book you are referring to is merely considered by some
> to be the best of the bad JavaScript books known around here.


Sorry, that was Flanagan's book. The book you are referring to is much
worse than that. Google is your friend. [psf 6.1]


PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      08-14-2007
On 14 Ago, 15:41, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> Thomas 'PointedEars' Lahn wrote:
> > josh wrote:
> >> [...] Thomas 'PointedEars' Lahn [...] wrote:
> >>> Since DOM stripting is inherently attached to the markup it operates on,
> >>> either you have read utter nonsense or you have completely misunderstood
> >>> what was written. If the former and it was a book, rip it apart, shredder
> >>> it, throw it in the trash can, burn it, and sue the author for being
> >>> incompetent.
> >> I quote the exact phrase taken by JavaScript Bible Sixth Ed and I
> >> think that this is a great book and the author Danny is also a professional
> >> javascript programmer!

>
> > You are wrong. The book you are referring to is merely considered by some
> > to be the best of the bad JavaScript books known around here.

>
> Sorry, that was Flanagan's book. The book you are referring to is much
> worse than that. Google is your friend. [psf 6.1]
>
> PointedEars
> --
> "Use any version of Microsoft Frontpage to create your site. (This won't
> prevent people from viewing your source, but no one will want to steal it.)"
> -- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Really? Please link me where is written and who wrote review! I don't
seem so!
and which are for you best javascript books? or the books where did
you study?
I'm very curious!

 
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
About Event Handlers, Delegates and Scopes Alex MCSD 6 05-21-2004 01:49 AM
Debug step into event handlers Robert Millman ASP .Net 0 07-29-2003 03:24 PM
wiring up html attribute declared event handlers Oisin Grehan ASP .Net 3 07-15-2003 10:27 AM
Re: crazy event handlers! Trevor Hartman ASP .Net 2 06-26-2003 01:53 AM
Dynamic Buttons and Event Handlers, revisited Boban Dragojlovic ASP .Net 0 06-24-2003 01:51 PM



Advertisments