0

It seems I understand the privacy-restrictions of visited links, but I can't understand why my code doesn't work properly.

Could be tested here: https://jsfiddle.net/xqLvyrnb/

<style>
    div { padding: 1em; }
    .inner { background-color: greenyellow; }
    .outer { background-color: yellow; }

    a:link { color: rgb(0, 138, 206); }
    a:visited { color: rgb(180, 0, 180); }
</style>

<div class="outer">
    <div class="inner">
        <a href="https://gooooooooogle.com">Probably unvisited link</a>
    </div>
</div>

<div class="outer">
    <div class="inner">
        <a href="https://stackoverflow.com">Probably visited link</a>
    </div>
</div>

<script>
    // Uncomment one of these 2 lines:
    //var target_elements = document.querySelectorAll('.inner a:link');    // Works
    //var target_elements = document.querySelectorAll('.inner a:visited'); // Doesn't work!
    for (i = 0; i < target_elements.length; i++) {
        target_elements[i].parentNode.parentNode.style.backgroundColor = 'gray';
    }
</script>

Why it's strange for me? Because we don't use styling for a:visited. We use styling for parent element (and it doesn't work). How it could be fixed?

john c. j.
  • 725
  • 5
  • 28
  • 81
  • You can't select/detect `visited` with Javascript in any way - if you could, that would be a massive security issue. You can only assign CSS styles to the document and let the user's browser display them accordingly. – CertainPerformance Apr 21 '18 at 20:30
  • @CertainPerformance Hm. For some reason I was sure that it's not possible with jQuery (if I recall correctly, jQuery have some problems with pseudo-classes). But I was sure that there are no problems to work with visited links via plain JS. Probably I was wrong. – john c. j. Apr 21 '18 at 20:34
  • 1
    https://stackoverflow.com/questions/7290959/how-can-i-detect-visited-and-unvisited-links-on-a-page?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – romeuBraga Apr 21 '18 at 20:38
  • @romeuBraga Also: https://stackoverflow.com/questions/1210871/use-jquery-to-select-visited-links Ok, probably I will close this question in a few days. – john c. j. Apr 21 '18 at 20:44
  • Look at the answer with 30 upvotes in this link that you post – romeuBraga Apr 21 '18 at 20:47
  • @romeuBraga Yes, I already upvoted it before posting my previous comment. That's why I said I will close my question. – john c. j. Apr 21 '18 at 20:48
  • Possible duplicate of [Use jQuery to Select Visited Links](https://stackoverflow.com/questions/1210871/use-jquery-to-select-visited-links) – john c. j. Apr 21 '18 at 21:40

0 Answers0