SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trigger visited link pseudoclass without loading page

    Hi,

    I'm using some javascript to hide/show an ordered list with the click of a link. I am trying to figure out a way for the link click to register so that the visited pseudoclass will be applied, but not have the browser refresh since this is a pure client side activity.

    My initial attempt was to have the link be a named link and then have the javascript onclick event return false, ie <a href="#linkid" onclick="hideList();return true;" ... but this causes the browser window to scroll slightly in an attempt to find #linkid. I want the browser window to not move at all with the click. If I try to return false, then the link never gets picked up and the visited psuedoclass is never applied.

    Any ideas for how I can achieve this? Does the browser determine visited links from the browser history? Is it possible to add entries to it? TIA.

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it work if you set the href attribute as "#" ... i.e. just the hash ...?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JimmyP View Post
    Does it work if you set the href attribute as "#" ... i.e. just the hash ...?
    Not quite. I need each link in the list to be unique, hence the IDs. If I just use # all the links get colored.

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of relying on a pseudo-class change why not change the class onclick? (and then define that class in your stylesheet).
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JimmyP View Post
    Instead of relying on a pseudo-class change why not change the class onclick? (and then define that class in your stylesheet).
    I'm trying to preserve the visit history. If I apply the change onclick, then it only applies for that session right?

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, the browser uses its history to determine what is visited and what isn't. I think your best bet is to use CSS:
    Code CSS:
    a:visited, a.listHidden {
      color:purple;
      font-weight:bold;
    }
    Then you can do this with your javascript (lnk is the link in question):
    Code Javascript:
    lnk.onclick = function(e) {
      if (!e) window.event.returnValue = false;
      else e.preventDefault();
      this.className = 'listHidden'; // This will overwrite any other classnames previously there
      hideList();
    }
    The first two lines in the function will stop the browser looking for linkid (same effect as return false in inline javascript, which should be avoided).

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I apply the change onclick, then it only applies for that session right?
    Yes, it would only apply for that session.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Yes, it would only apply for that session.
    Thanks, I'll resort to that if I have to, but really trying to find a persistent solution.

  9. #9
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The only way to force a browser to put something in its history without making the user leave the current location is to use iframes. This is what Google Maps does. It doesn't seem a like a good solution for your case though - overkill.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •