This approach disabled for security reasons.
This has been confirmed in all major browsers: none support
querySelector("a:visited")
any longer.As it is possible to retrieve a visitor’s history by checking for visited links, certain measures have been taken by browser vendors to prevent this.
However, there is a workaround without using the :visited
selector:
I found a workaround utilising JavaScript
LocalStorage
This approach stores links clicked by the user in
LocalStorage
and adds the classvisited
to the parent of the<a>
element:
function check_visited_links() {
var visited_links = JSON.parse(localStorage.getItem('visited_links')) || [];
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var that = links[i];
that.onclick = function() {
var clicked_url = this.href;
if (visited_links.indexOf(clicked_url) == -1) {
visited_links.push(clicked_url);
localStorage.setItem('visited_links', JSON.stringify(visited_links));
}
}
if (visited_links.indexOf(that.href) !== -1) {
that.parentNode.className += ' visited';
}
}
}