21

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(); 
});
TrippRitter
  • 305
  • 2
  • 10
  • 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 Sep 20 '13 at 16:21
  • I gave it a shot but it still doesn't work. Things work as expected if the exact same markup is rendered when the page is loaded rather than loaded via javascript. Example: http://jsfiddle.net/7nj3x/5/ – TrippRitter Sep 20 '13 at 16:58
  • 1
    looks like jQuery encountered this too: http://bugs.jquery.com/ticket/7439 Doesn't look like they ever resolved it though. – Dave Sep 20 '13 at 17:05
  • Not sure if this will help. Have a look at a Stack Overflow entry I found: http://stackoverflow.com/questions/8331688/why-doesnt-this-avisited-css-style-work Might answer your question. Regards, Barry – Barry Sep 21 '13 at 05:37
  • Maybe this link can help: http://answers.microsoft.com/en-us/ie/forum/ie9-windows_vista/ie9-has-lost-the-color-change-for-previously/56d006eb-2c0b-4deb-afd7-c5ed1215e08e It seems to be related with something not implemented in the IE Style in the plugin. Maybe you can contact the developer and report the bug. – Guillermo Sep 23 '13 at 23:18
  • test it on ie 10 it shows visited link...don't know about older version – Marko Vasic Sep 25 '13 at 14:45
  • 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 Sep 25 '13 at 16:16
  • 1
    Have you tried using http://balupton.github.io/jquery-history/demo? It allows you to programmatically push items into to the browser's history. It supports IE9. – Diego Sep 26 '13 at 00:53
  • You could implement a manual style set to handle this. It does look like an IE issue. – Daniel Tate Sep 26 '13 at 01:18
  • 1
    It could be your vlink is the same color as a unvisited link. – Matthew Sep 27 '13 at 19:33
  • The jsfiddle above did not work in Win7/IE10: https://saucelabs.com/tests/47d528e0a2954f79b658701e08d36f8f – ledlogic Jan 03 '14 at 14:38
  • This has come up before specifically for IE9, but usually related to eventListeners/click handlers, so I haven't ran this to see if it carries over in a hash usage, so consider it an additional data point.The work around in those cases has been to add a `preventDefault` event call, as well as setting `event.returnValue` to `false`. – shadowstorm Mar 25 '15 at 18:24

7 Answers7

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]-->
bpslolk
  • 138
  • 1
  • 7
0

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;}
AJames
  • 64
  • 3
  • I used a work around similar to this but rather than storing visited states in a hidden input I stored them in local storage. See [TrippRitter Sept 24](http://stackoverflow.com/questions/18919388/ie-not-marking-links-as-visited-when-rendered-via-javascript#comment28086057_18919388) – TrippRitter Oct 04 '13 at 14:28
  • Not sure how much your putting into local storage but you may want to check this out as well if you run into performance issues: https://hacks.mozilla.org/2012/03/there-is-no-simple-solution-for-local-storage/ – AJames Oct 04 '13 at 18:06
0

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(); 
});
Aleuck
  • 264
  • 1
  • 11
0

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.

Ibio Tan
  • 101
  • 1
  • 8
0

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);
7kasper
  • 35
  • 8
0
 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]-->
Sri Sri M
  • 9
  • 4
-1

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.

  • The exploit you are describing is that you can't add a ton of links to a page like pornhub.com and then send which links the browser thinks are visited to your private server for blackmail purposes. What the OP is describing is different which is why I believe you were downvoted. – nym Aug 29 '17 at 21:15