views:

36

answers:

2

Hi, I'm currently developing an ajax application and I'm looking for a feature that lets me intercept all static and dynamic links using javascript. The links look like these:

<a href="link1/">link 1</a>
<a href="link2/">link 2</a>
etc.

I then want the browser to redirect to: current.page/#link1/ rather than current.page/link1/. I'm using jQuery, so the live() function is an option, however using that as a solution just seems rather sluggish to me(am I hysterical?). If there is a way to intercept ALL links on a page, maybe through detecting a change in the address, that would greatly help. I've tried a few plugins for jQuery (jQuery address & SWFaddress) but they only seem to have event handlers that respond to changes in anchor tags in the address. Any ideas?

thanks for your time

+1  A: 

Don't worry to much about performance unless you have to. Often the elegant solution is also the right one.

I would use jQuerys live function, bind to the click event and rewrite the link as it is being clicked on.

Hope this helps, Egil.

Egil Hansen
Thanks for the quick reply!
soren.qvist
+1  A: 

What the live function does is it binds an event handler to the document, which catches all click events and then detects all clicks that match the selector, in your case the link elements. This is the most efficient way of catching all link clicks.

Matti Virkkunen
Thanks! Maybe you could tell me which selector is the quickest, one which select a list of individual div id's of a document, or one that selects all div's with a given class?
soren.qvist
soren.qvist: ID selectors are generally the fastest.
Matti Virkkunen