> I currently have a problem with the site I am designing, I'm using jQuery to > load the content via AJAX, however. The script I'm using to watch for when > the user clicks a link is: > > function watchLinks() > { > $("a").click(function() > { > var youWantToGoTo = > $(this).attr('href').substr(0,$(this).attr('href').length-4); // Determine > destination > openPage(youWantToGoTo); // Open destination > window.location.hash = youWantToGoTo; // Set the url # > return false; > }); > }; > > currently I call the function on the $(document).ready but this means that I > cannot watch the links that are loaded dynamically. > > eg when the users loads the 'portfolio' page the watchLinks() function isn't > active for the links which were loaded. > > I have tried setting watchLinks() as the callback but when the user quickly > switches between pages the javascript seems to slow down to a snails pace > and pages don't load for a fairly long period of time. > > Is there a more efficient method?
Instead of binding event listeners to the anchors use event delegation instead: http://www.learningjquery.com/2008/03/working-with-events-part-1