using javascript to mark a link as visited

前端 未结 5 1578
孤独总比滥情好
孤独总比滥情好 2020-12-06 01:17

FF2 (at least) doesn\'t mark as link as :visited if it triggers the onclick handler without following the href. I\'m using onclick to fetch data from a server and modify th

5条回答
  •  情话喂你
    2020-12-06 01:50

    The only workaround I know of would be something like the following.

    Say, your visited links are red:

    link
    

    But that doesn't mean that when the page is reloaded, the links are still marked visited.

    To achieve this, I'd suggest give all links IDs, which are of course unique across your entire app, or namespaced per page. In your onclick, you'll trigger another method, which saves the link's ID to a cookie.

    The most easiest would be a comma-separated list, which you can split() before reading. Which is what you do when the page is reloaded. When it's split, you iterate over all IDs and set the color on your links.

    E.g, using jQuery:

    // onclick
    function saveID(id) {
      if ($.cookie('idCookie')) {
        $.cookie('idCookie', $.cookie('idCookie') + "," + id);
      } else {
        $.cookie('idCookie', id);
      }
    }
    
    // make all links colored
    function setVisted() {
      var idArray = $.cookie('idCookie').split(',');
      for (var x=0; x

    I haven't tested this code, but it should get you started and give you an idea. If you get lucky, it's paste and win. ;-) I also haven't researched how much you can store in a cookie and what the performance implications are, or what other restrictions apply, also see my comments.

提交回复
热议问题