Improved javascript logic for links