简体   繁体   English

使用 javascript 将链接标记为已访问

[英]using javascript to mark a link as visited

FF2 (at least) doesn't mark as link as :visited if it triggers the onclick handler without following the href. FF2(至少)不会将链接标记为 :visited 如果它在不遵循 href 的情况下触发 onclick 处理程序。 I'm using onclick to fetch data from a server and modify the page and the link styling seems appropriate here.我正在使用 onclick 从服务器获取数据并修改页面,链接样式在这里似乎很合适。 But the link is not marked as visited.但该链接未标记为已访问。

Is there a cross-browser way to mark the link as visited?是否有跨浏览器的方式将链接标记为已访问? Failing that, is there a way to determine the browser's a:visited styling and apply it to the link?如果失败,有没有办法确定浏览器的 a:visited 样式并将其应用于链接?

Thanks to all who replied.感谢所有回答的人。

Looks like the answers are:看起来答案是:

  • Is there a cross-browser way to mark the link as visited?是否有跨浏览器的方式将链接标记为已访问?
    No, there's no way to do this.不,没有办法做到这一点。 Links are identified as visited if the href is in the browser history.如果 href 在浏览器历史记录中,则链接被标识为已访问。
  • Is there a way to determine the browser's a:visited styling?有没有办法确定浏览器的 a:visited 样式?
    No, not via javascript alone.不,不能单独通过 javascript。

Here is how I did it.这是我如何做到的。 Only works in browsers that support HTML5 history api.仅适用于支持 HTML5 history api 的浏览器

// store the current URL
current_url = window.location.href

// use replaceState to push a new entry into the browser's history
history.replaceState({},"",desired_url)

// use replaceState again to reset the URL
history.replaceState({},"",current_url)

Using replaceState means that the back button won't be affected.使用 replaceState 意味着后退按钮不会受到影响。

The only workaround I know of would be something like the following.我所知道的唯一解决方法如下。

Say, your visited links are red:比如说,您访问过的链接是红色的:

<a href="#" onclick="someEvent();this.style.color='#ff0000'">link</a>

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.为了实现这一点,我建议提供所有链接 ID,它们当然在整个应用程序中都是唯一的,或者每个页面都有命名空间。 In your onclick , you'll trigger another method, which saves the link's ID to a cookie.在您的onclick ,您将触发另一个方法,该方法将链接的 ID 保存到 cookie。

The most easiest would be a comma-separated list, which you can split() before reading.最简单的是逗号分隔的列表,您可以在阅读之前split() 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.拆分后,您将遍历所有 ID 并在链接上设置颜色。

Eg, using jQuery:例如,使用 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<idArray.length; x++) {
    $('#' + idArray[x]).css('color', '#ff0000');
  }
}

// assign saveID()
$(document).ready(function(){
  $('a').click(function(){
    saveId($(this).attr('id'));
  });
  setVisited();
});

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 .如果幸运的话,它就是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. ;-) 我也没有研究你可以在 cookie 中存储多少以及性能影响是什么,或者还有哪些其他限制适用,也请参阅我的评论。

应用与 :visited 具有相同定义的类。

Strictly speaking, there's no such thing as a "visited" state for individual links.严格来说,单个链接没有“已访问”状态这样的东西。 It's the URLs themselves that are interpreted as "visited" by the browser. URL 本身被浏览器解释为“访问过”。 Any links that point at a URL in the browser's history will receive styling as defined by the :visited pseudo-style in your CSS.任何指向浏览器历史记录中 URL 的链接都将接收由 CSS 中的 :visited 伪样式定义的样式。

You could try to fake it by setting the location of a hidden iframe to the desired URL, but that won't force the current page to re-draw so I doubt you'd see the :visited style updates w/oa refresh.您可以尝试通过将隐藏的 iframe 的位置设置为所需的 URL 来伪造它,但这不会强制当前页面重新绘制,所以我怀疑您是否会看到 :visited 样式更新 w/oa 刷新。

For the 2nd part of your question, I'd probably go with Jordan Jones' answer.对于您问题的第二部分,我可能会同意 Jordan Jones 的回答。

Is there a way to determine the browser's a:visited styling?有没有办法确定浏览器的 a:visited 样式?

I would say yes since the current document is visited and you can find it's link color like this:-我会说是,因为访问了当前文档,您可以找到它的链接颜色,如下所示:-

alert(mys_getLinkColor(top.location))


function mys_getLinkColor(href) {
var x, body, res=''
x = document.createElement('a')
x.href = href
body = document.getElementsByTagName('body')[0]
body.appendChild(x)
if(x.currentStyle) {
   res = x.currentStyle.color
}
else if(window.getComputedStyle) {
   res = window.getComputedStyle(x,null).color
}
return mys_rgbToHexColor(res) }


function mys_rgbToHexColor(v) { 
// E.g. 'rgb(5,2,11)' converts to "#05020b". All other formats returned unchanged.
var i
v = v.split('(')
if(!v[1]) return v[0]
v = v[1].replace(/ /g, '').replace(/\)/, '')
v = v.split(',')
for(i=0; i<v.length; i++) {
   v[i] = Number(v[i]).toString(16)
   if(v[i].length==1) v[i] = '0' + v[i]
}
return '#'+v.join('')}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM