views:

498

answers:

6

I have html-pages with links where i want to attach a function to their onclick-event. One way to do it is of course:

<a href="save.php" onclick="save(); return false;" rel="save">Save</a>

But I know this is not the best practice. So instead I wait for window.onload, loop through the links and attach the save-function to the links with rel="save". The problem with this is that it waits until the whole page has finished loading which can be several seconds after the link is displayed and clickable.

So is there another way to do this? Avoiding onclick in the html but that makes it work immediately when the link is rendered.

+1  A: 

You could try DOMContentLoaded event instead of load. IE also gives you the defer attribute for script tags, which defers execution until the DOM is loaded. If those don't work for you, then you are stuck with the solutions you mention, as far as I know.

geowa4
This is part of the right answer. jQuery's famous ready event uses this along with some other options to fire events at the optimal time.
Rafe
+1  A: 

In your case, you can just leave that as it is. Stick to the simplest possible thing, even if it is not the general best practice.

Narayan Raman
My case here was just an example, there are many times i would need this functionality. And the best pratices is what i'm looking for.
Martin
A: 

I don't know if this is appropriate for your solution, but you could insert script immediately below the are with the links you need altered. This script would not be wrapped in a function, allowing the browser to execute it immediately when seen. The effect is that you can run script before the full page is loaded, altering only the items that exist above the script being run. (If you reference something below the script, it will fail.)

BTW, this is almost certainly not a best practice, and some would probably label it a worst practice.

John Fisher
Yeah I thought about that, but as you said this is probably the worst practice ;)
Martin
+3  A: 

Internet Explorer has a handy attribute for <script> tags called defer. It's for delaying the parsing of a script until the document has finished loading. For other browsers that support it, you can use DOMContentLoaded, as someone else suggested and for browsers that don't support either you can fall back to onload.

<script type="text/javascript" defer>
//- Run this code when the DOM parsing has completed
</script>

I did a quick Google search for "DOMContentLoaded defer" and found the following page that might help:

http://tanny.ica.com/ica/tko/tkoblog.nsf/dx/domcontentloaded-event-for-browsers

Andy E
A: 

How about this?

<a href="javascript:void(save());">Save</a>

Note: This solution requires to users to have Javascript enabled. Not exactly best practice, but may be suitable for your scenario.

Josh Stodola
This is almost the opposite of what I want. Everything works without javascript now, I just want to speed up the initialization of events.Is there any case when this javascript:-thing is better than to use onclick?
Martin
It's better when you don't want to have delayed event handlers. Other than that, your only way to speed it up would be to use a "content loaded" event as opposed to window.onload
Josh Stodola
Yes but is it faster than <a href="" onclick="save()">Save</a> ?
Martin
It's faster because you don't have to assign an event handler. As soon as the link appears in the browser, the Javascript will immediately be 100% functional, assuming that your save function is being imported earlier in the markup (like in the HEAD section).
Josh Stodola
A: 

The ideal here would be to use the ideas of Unobtrusive Javascript.

In this way, if the Javascript isn't loaded the link would still do something. It's a link right, so it leads the user to another piece of content? - this should work without Javascript. And if the functionality attached to the links can ONLY work with Javascript you should create and insert them into the DOM with Javascript (they aren't clickable if they aren't there...).

(Otherwise how about delegating the click event to a wrapper element? Does that work before the element is complete?)

edit: Oh, and "save" sounds very much like it ought to be a button in a form rather than a link. The Unobtrusive JS stuff still applies though.

edeverett
Yes, everything works without javascript. I never rely on javascript. But it would be nice to be able to take advantage of for example the pros of ajax without having to wait 10 seconds for everything to load.Don't worry about "save", I just made that up as an example. I have tons of features that would benefit if this problem was solved.
Martin
Good stuff. Does it make sense to use event delegation while the document is still loading?: <div id="myDiv"><script>//add event to myDiv</script> ...loading stuff...
edeverett