473,385 Members | 1,806 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,385 software developers and data experts.

Attaching functions to events, from external JS files

Hello.

I am a web developer very worried about "bloat code" and "languages
mixture". So, since some time, I always try to avoid completely the use
of javascript in XHTML/HTML files. This leads to me to hate, also, any
event (onload, onchange, ...) instanciated in the element tag itself, like:

<input type="text" onkeyup="return doSomething();" />

I would replace this code above with an input element marked with an
"id" attribute, and append a JS file to the header with the following
content:

function doSomething(){
[...]
}

function ApplyEvents(){
document.getElementById("mytextinput").onkeyup = doSomething;
}

window.onload = ApplyEvents;
I think that all people who read this would agree that this method is
very smart and gentle.

But it has a very big problem: sometimes, when the XHTML/HTML page is
very large, the ApplyEvents function gets called to late. For example,
when the page is loaded, the user has usually been able to use some
HTML/XHTML controls so as to cause the page to fire some events, but
they are ignored.

I have thought also about using <script> elements just after each tag
element to which I want to apply an event, but I think this is a bit dirty.

Do you know a better method to apply events without mixing javascript
with the markup or using events on the XHTML/HTML source? If I was a
person in charge of suggesting XHTML 2.0 capabilities, I would propose a
new event for the window, for example: onnodeload. This way I could
apply events whenever a new tag has arrived from the server to the
rendering client.

Regards,

Andrew

--
Jul 23 '05 #1
5 2341
On Fri, 01 Jul 2005 20:50:10 +0200, knocte
<kn****@NO-SPAM-PLEASE-gmail.com> wrote:
I am a web developer very worried about "bloat code" and "languages
mixture".
Why?
So, since some time, I always try to avoid completely the use
of javascript in XHTML/HTML files. This leads to me to hate, also, any
event (onload, onchange, ...) instanciated in the element tag itself, like:
A big mistake, as you've noticed below, if you do not have it in the
code, you have to hide the controls from all users, or accept that
your page is going to behave inconsistently. Why not just accept the
limitations of your medium, and use the sensible methods - your users
will be much happier, and you can develop techniques that work.
<input type="text" onkeyup="return doSomething();" />

I would replace this code above with an input element marked with an
"id" attribute, and append a JS file to the header with the following
content:

function ApplyEvents(){
document.getElementById("mytextinput").onkeyup = doSomething;
}
Using DOM 1 getElementById, to access form elements is a bad idea, DOM
0 (aswell as HTML DOM standard of course) document.forms method, is
considerably better supported.
window.onload = ApplyEvents;

I think that all people who read this would agree that this method is
very smart and gentle.
No, as you note below, it's disastrous, changing how your page behaves
best on onload is simply wrong, it fails the "consistency" rule of UI
- doing the same thing results in the same action.
I have thought also about using <script> elements just after each tag
element to which I want to apply an event, but I think this is a bit dirty.
Sure, the method is intrinsic events as they're called some places,
but they're the things you're trying to get rid of, maybe if you
explain more of your reason for getting rid of them...
If I was a
person in charge of suggesting XHTML 2.0 capabilities, I would propose a
new event for the window, for example: onnodeload.


Why haven't you then? the development of W3 standards happens in
semi-public, members of the Working Group certainly read and respond
to the mailing list. However there's no need for such an event, the
Mutation events already exist which would cover this - if they were
supported anywhere.

Jim.
Jul 23 '05 #2
Thanks for such a quick answer!

Jim Ley escribió:
On Fri, 01 Jul 2005 20:50:10 +0200, knocte
<kn****@NO-SPAM-PLEASE-gmail.com> wrote:
I am a web developer very worried about "bloat code" and "languages
mixture".
Why?


Maintainability reasons. If I set a discipline where programmers can add
many languages or layers/tiers in one file, I will end up with SQL in
ecmascript code. I want to avoid this at all.
So, since some time, I always try to avoid completely the use
of javascript in XHTML/HTML files. This leads to me to hate, also, any
event (onload, onchange, ...) instanciated in the element tag itself, like:


A big mistake, as you've noticed below, if you do not have it in the
code, you have to hide the controls from all users, or accept that
your page is going to behave inconsistently. Why not just accept the
limitations of your medium, and use the sensible methods - your users
will be much happier, and you can develop techniques that work.


Well, I respect your opinion, but I don't agree at this moment (unless
you convince me :) BTW, who are those which you are calling users? The
user of the web page? I think they won't bother about that.

Besides, taking your argument to the last consequence, we could state
that any javascript code outside the XHTML/HTML document (via <script>
elements) is evil.
<input type="text" onkeyup="return doSomething();" />

I would replace this code above with an input element marked with an
"id" attribute, and append a JS file to the header with the following
content:

function ApplyEvents(){
document.getElementById("mytextinput").onkeyup = doSomething;
}


Using DOM 1 getElementById, to access form elements is a bad idea, DOM
0 (aswell as HTML DOM standard of course) document.forms method, is
considerably better supported.


I know that older specifications are always more compatible. But my web
knowledge has led me to behave with the following rule:

- Make webs with javascript non required, so as to allow absolutely
obsolete or simple browsers.
- If javascript is used, assume that I am dealing with a very modern one.

Using this technique I think I succeed very well at my everyday work.
window.onload = ApplyEvents;

I think that all people who read this would agree that this method is
very smart and gentle.


No, as you note below, it's disastrous, changing how your page behaves
best on onload is simply wrong, it fails the "consistency" rule of UI
- doing the same thing results in the same action.


I think you are too proud to discuss about technology. I would never say
such subjective and rude terms in a technical discussion. Perhaps my
point is bad, but we are here to talk about it, not to make FUD's.

Of course, knowing the issue I have announced in this newsgroup, one
could say I don't design correct UI's, but what happens if I told you
that I use transition layers in my framework (div's with "LOADING..."
labels that get replaced with the UI when the page is loaded, and then,
when the onload event has already been called). Then you could answer:
so why did you ask this if you already had a solution? Because I want to
know if anyone else have other solutions (worse or better) to this issue.
I have thought also about using <script> elements just after each tag
element to which I want to apply an event, but I think this is a bit dirty.


Sure, the method is intrinsic events as they're called some places,
but they're the things you're trying to get rid of, maybe if you
explain more of your reason for getting rid of them...


I want to make a clean piece of software, where javascript is placed
only at one place and not at many places, I want a software that is easy
to change. I want a software that allows me to think that if I want to
change the client scripting, I will edit a .js file. I hope you
understand me more now.
If I was a
person in charge of suggesting XHTML 2.0 capabilities, I would propose a
new event for the window, for example: onnodeload.


Why haven't you then? the development of W3 standards happens in
semi-public, members of the Working Group certainly read and respond


Hehe, I already knew this, but I wanted to pass, firstly, the filter of
the javascript newsgroup :D
to the mailing list. However there's no need for such an event, the
Mutation events already exist which would cover this - if they were
supported anywhere.


Mmmm, could you point me to any URL about this?

Anyway, my wish would be to find a solution to this which was already
supported by most ecmascript interpreters.

Regards and thanks again for your help.

Andrew

--
Jul 23 '05 #3
On Fri, 01 Jul 2005 21:32:40 +0200, knocte
<kn****@NO-SPAM-PLEASE-gmail.com> wrote:
Maintainability reasons. If I set a discipline where programmers can add
many languages or layers/tiers in one file, I will end up with SQL in
ecmascript code. I want to avoid this at all.
Do you also keep all your HTML and CSS out of your script files? This
really starts to be difficult, you have to have all content you might
want to add in the main page, which makes fallback very difficult with
the non-script or CSS browsers.

Also of course, you have the <script> and <link> elements in the HTML,
so there's no genuine true seperation. If we could have true
seperation, without compromising on the usability issues, then I'd
agree with you, we should keep them seperate, but the compromises are
too severe.
A big mistake, as you've noticed below, if you do not have it in the
code, you have to hide the controls from all users, or accept that
your page is going to behave inconsistently. Why not just accept the
limitations of your medium, and use the sensible methods - your users
will be much happier, and you can develop techniques that work.


Well, I respect your opinion, but I don't agree at this moment (unless
you convince me :) BTW, who are those which you are calling users? The
user of the web page? I think they won't bother about that.


UI consistency is one of the key principles, if typing in a box does
something, it should always do the same thing, it shouldn't only do
that thing after page load.

For me, Google has javascript disabled because it doesn't understand
that onload is too late to do something, and google's a tiny page to
download, but with script enabled, I get inconsistent behaviour.
Besides, taking your argument to the last consequence, we could state
that any javascript code outside the XHTML/HTML document (via <script>
elements) is evil.
How so? Certainly you need to be careful that your document still
functions if any remote resources fail to load (so you need stub
functions or to check in your intrinsic events), however there's no
consistency change, the document works the same way, in the same
instance. I agree there's a potential problem if the user reloads and
this time the external resource succeeds whereas it previously failed,
but that's not a big risk, and I don't think the monolithic page is a
good response to that.

Remember my arguments are about pragmatism in working with what we
have, rather than some ideal model.
- Make webs with javascript non required, so as to allow absolutely
obsolete or simple browsers.
An excellent idea, however the script above would just error in many
browsers - script errors popping up are not a website that's working,
and certainly are a bigger issue than mixing languages in a page?
I think you are too proud to discuss about technology. I would never say
such subjective and rude terms in a technical discussion.
Rude? I'm sorry you were offended, but I don't get what offended you,
lack of UI consistency is a problem, a big problem, you recognised it
yourself, it's why you posted!
Of course, knowing the issue I have announced in this newsgroup, one
could say I don't design correct UI's, but what happens if I told you
that I use transition layers in my framework (div's with "LOADING..."
labels that get replaced with the UI when the page is loaded, and then,
when the onload event has already been called).
The problem with these approaches, which do get around the consistency
issue, is that they fail the fallback issue - how do you both show the
message, and leave the page working for people without sufficiently
advanced script supported?
I want to make a clean piece of software, where javascript is placed
only at one place and not at many places, I want a software that is easy
to change. I want a software that allows me to think that if I want to
change the client scripting, I will edit a .js file. I hope you
understand me more now.
The problem here is that you don't really get that what you're
replacing is a dependancy in the script on "onclick='something()'"
with a dependancy on an ID, is there really that much independance
between:

<h1 id="chicken" ... >
<h1 onclick="chickenClicked()" ... >

Sure, you could attach other events to the first one than onclick, but
that's all, if you decide you want to change which element you want to
respond to a click you still need to change the HTML.

There are good future technologies for this, particularly XBL, where
you truly can seperate the behaviour from the mark-up, but we don't
have it yet, I don't think it's a good idea to compromise on the
usability of your application.
to the mailing list. However there's no need for such an event, the
Mutation events already exist which would cover this - if they were
supported anywhere.


Mmmm, could you point me to any URL about this?


<URL:
http://www.w3.org/TR/DOM-Level-2-Eve...mutationevents Anyway, my wish would be to find a solution to this which was already
supported by most ecmascript interpreters.


There isn't one :-) there's better than onload available in some,
there's exactly what you want for IE if behaviors are enabled (but
they have a whole raft of other problems), but in general there's
nothing that will give you what you want.

So your solutions are, leave inconsistent UI's around.
Try and solve the "loading..." methods problems with fallback when
CSS/javascript is unavailable weakly supported.

Jim.
Jul 23 '05 #4
VK
The external event listeners' issues were discussed many times here.
You nay want to read this (and similar - use "event" keyword to search;
I just couldn't pass the opportunity to post this link w/o formally
top-posting :-)
<http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/083ed9630b81dee9/cd81fab7ffdfadd7?hl=en#cd81fab7ffdfadd7>

The ergonomics studies tell us that users prefer to wait longer but get
the final result "in one chunk" rather than to be forced to see the
creation process.

In the partucular, they would prefer to see "Loading..." sign as long
as it's needed, but when the form appears, it should be fully usable
from the very first moment. They would be disturbed by a form that
appears part-by-part but is not fully usable until the last part
arrived. It's a side-effect of the common human behavioral template:
"If you can give it to me, then *give* it to me. Otherwise don't show
it to me".

So in your approach I would suggest to stay on "onload" event to attach
all needed events and *to display* the form only after all preparative
works are done.

Jul 23 '05 #5

Both inline/explicit event handlers and implicit/indirect ones are
necessary, one no more important than the other, each has their own use,
your opinion is understood and noted, however is just opinion and as
entitled as you may be, doesn't quite equate to practical for most
scenarios. If you are on server end, you can have the server language put
it inline, if you don't like it inline, you have have js do it indirect
binding by means of a function per parent on children bubbling or per node
itself by using IDs. As for the onnodeload, onload on the body takes care
of the whole document being loaded, not just the node, as your plight on
the user being able to click on an event, well, that is something YOU as
programmer should address, as it's simple to do, and no try to pass the
buck to the API. As for the 'evil' call on code outside the <script>
section, well, even though in tags, is technically seen even by html as
js, not html. You need a centralized system for the object handling, and
is understood for your UI, but as much as I dislike to quote donald
rumsfeld on his buck-passing response a while ago, "you go with the army
you have, no the one you wish you had or the one you think you should have
had".

I think you're already using the optimal technique for your Divs and UIs,
as for the user clicking anywhere, you just need code it to not do so and
do use inline event handlers, they're there to help you, not to take you
astray.
Danny
On Fri, 01 Jul 2005 11:50:10 -0700, knocte
<kn****@NO-SPAM-PLEASE-gmail.com> wrote:

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #6

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

Similar topics

1
by: George Ziniewicz | last post by:
.. I have an html file with javascript functions in the <head> section that work fine when called from events or instanced as objects. When I move the functions to an external js file they don't...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
6
by: John Ratliff | last post by:
I was reading the C++ FAQ Lite about inline functions, and it says the following (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7) " It's usually imperative that the...
3
by: Michael Schneider | last post by:
Hello All, I am comming back to python after being away for several years. I would like to use weak refs in an observer pattern implementation. The problme that I have seems to be that...
3
by: Rob Davis | last post by:
I am familiar with VBA and the manual method of attaching/linking external data tables (File, Get External Data etc). I am also familiar with opening tables which exist in the current Access file,...
3
by: Kevin Burton | last post by:
I am trying to use managed C++ but I am getting the following link errors: Metadata file 'D:\Projects\Visa\AddressVerification\AddressVerificat...
5
by: | last post by:
I am wondering what the best method of attaching custom Events to custom WebUserControls are. I cannot seem to find the proper terminology to expand my research. Basicallly I have a custom user...
1
by: metiu | last post by:
Hi all, maybe it's an old question, but I couldn't find an answer... let's say I have this kind of directory structure for project foo: /foo/includes/mod1.h (exported by mod1)...
3
by: jason.cipriani | last post by:
I'm in a rather strange situation. I have a class that does some stuff and uses a buffer to hold some data in; the details aren't important but the buffer is an std::vector: class Something {...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.