最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - IE not marking links as "visited" when rendered via javascript - Stack Overflow

programmeradmin2浏览0评论

I am working with a site where all content is rendered via ajax postbacks using jquery. I am using Ben Alman's hashchange (/) to manage the hash history which allows me to bookmark pages, use the back button etc... Everything works perfectly on everything but IE 9 of course. In IE there is a small issue with "visited" links not being marked as visited. You can see that the link turns purple(visited) for a split second after you click it before the new content is loaded. But once you click the back button the link appears as though it has never been visited. Here is a jfiddle example of what I am talking about: /

Here is the jsfiddle code assuming you have jquery and the hashchange plugin referenced in head:

$(function(){
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  $(window).hashchange( function(){
    alert("Hash changed to:"+location.hash);  
    var hash = location.hash;
    // Set the page title based on the hash.
    document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
    //simulate body being rendered by ajax callback 
      if(hash == ""){  
        $("body").html("<p id='nav'><a href='#test1'>test 1</a> <a href='#test2'>test 2</a> <a href='#test3'>test 3</a></p>");
      }
      else{
        $("body").html("Right click within this pane and select \"Back\".");  
      }
  })
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).hashchange(); 
});

I am working with a site where all content is rendered via ajax postbacks using jquery. I am using Ben Alman's hashchange (http://benalman.com/projects/jquery-hashchange-plugin/) to manage the hash history which allows me to bookmark pages, use the back button etc... Everything works perfectly on everything but IE 9 of course. In IE there is a small issue with "visited" links not being marked as visited. You can see that the link turns purple(visited) for a split second after you click it before the new content is loaded. But once you click the back button the link appears as though it has never been visited. Here is a jfiddle example of what I am talking about: http://jsfiddle.net/7nj3x/3/

Here is the jsfiddle code assuming you have jquery and the hashchange plugin referenced in head:

$(function(){
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  $(window).hashchange( function(){
    alert("Hash changed to:"+location.hash);  
    var hash = location.hash;
    // Set the page title based on the hash.
    document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
    //simulate body being rendered by ajax callback 
      if(hash == ""){  
        $("body").html("<p id='nav'><a href='#test1'>test 1</a> <a href='#test2'>test 2</a> <a href='#test3'>test 3</a></p>");
      }
      else{
        $("body").html("Right click within this pane and select \"Back\".");  
      }
  })
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).hashchange(); 
});
Share Improve this question edited Sep 20, 2013 at 16:10 TrippRitter asked Sep 20, 2013 at 14:34 TrippRitterTrippRitter 3013 silver badges10 bronze badges 12
  • Maybe IE requires a valid target for the link (e.g. an a tag with the correct name). I don't have IE to test on here, so it's just a guess. – Dave Commented Sep 20, 2013 at 16:21
  • 1 looks like jQuery encountered this too: bugs.jquery.com/ticket/7439 Doesn't look like they ever resolved it though. – Dave Commented Sep 20, 2013 at 17:05
  • 2 I believe it works on IE 10. It's IE 9 and IE 8 that seem to have the problem. I got it working using a bit of javascript where I save all clicked urls to local storage and iterate through them after each ajax postback applying a css rule via jquery against any link on the page that is found in local storage. It's a hack for sure, but it works. – TrippRitter Commented Sep 25, 2013 at 16:16
  • 1 Have you tried using balupton.github.io/jquery-history/demo? It allows you to programmatically push items into to the browser's history. It supports IE9. – Diego Commented Sep 26, 2013 at 0:53
  • 1 It could be your vlink is the same color as a unvisited link. – Matthew Commented Sep 27, 2013 at 19:33
 |  Show 7 more comments

7 Answers 7

Reset to default 2

You can simply use IE conditional comments to load a specific style:

<!--[if IE]>
  a:visited {
     padding-left: 8px;
     background: url(images/checkmark.gif) no-repeat scroll 0 0;
}
<![endif]-->

Why not setup a code block only to be used by IE that sets the value of a hidden input tag to reflect the click behavior. If a link is clicked you could set the value of the input tag equal to that link id and allow you js to update the elements class to reflect the change.

HTML if IE
<input type="hidden" id="clicked_link" />


JQuery JS if IE
$(function() {
    $(a).click(function() {
        $(this).attr('id').addClass('visited_link_class');
    });
});

CSS
.visited_link_class { color:#your color;}

Maybe if you create the proper elements and building a DOM segment before appending it to the document.

Not sure it would work, can't test it here, but here goes my try adapting your code.

$(function(){
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  $(window).hashchange( function(){
    alert("Hash changed to:"+location.hash);  
    var hash = location.hash;
    // Set the page title based on the hash.
    document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
    //simulate body being rendered by ajax callback 
      if(hash == ""){  
        $("body").html(
          $("<p>").id("nav")
            .append($("<a>")
              .attr("href","#test1")
              .text("teste 1"))
            .append($("<a>")
              .attr("href","#test2")
              .text("test 2"))
            .append($("<a>")
              .attr("href","#test3")
              .text("test 3"))
        );
      }
      else{
        $("body").text("Right click within this pane and select \"Back\".");  
      }
  })
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).hashchange(); 
});

Try to consider css LVHA roles, which means the order of an a tag pseudo class matters.

First time to define those class:

  • A:link
  • A:visited
  • A:hover
  • A:active

If this still did not solve your problem, you can use another js router(hashchange): https://github.com/flatiron/director I used this one a lot and it works perfectly in many situations.

An option would be to also fake the browser history using the HTML5 history API. This way only after deleting the browser history the link will be 'unvisited'.

Like said on this useful page:

[...] method above switches out the URL in the address bar with '/hello' despite no assets being requested and the window remaining on the same page. Yet there is a problem here. Upon hitting the back button we'll find that we don't return to the URL of this article but instead we'll go back to whatever page we were on before. This is because replaceState does not manipulate the browser's history, it simply replaces the current URL in the address bar.

So like also mentioned on that page you'll have to do a:

history.pushState(null, null, hash);
 You can simply use IE conditional comments to load a specific style:    
<!--[if IE]>
      a:visited {
         padding-left: 8px;
         background: url(images/checkmark.gif) no-repeat scroll 0 0;
    }
<![endif]-->

This is a security feature in ie. The functionality of :visited has been restricted in many modern browsers to prevent CSS exploit. Hence, there's no workaround for this issue.

发布评论

评论列表(0)

  1. 暂无评论