使用 javascript 将链接标记为已访问 [英] using javascript to mark a link as visited
问题描述
FF2(至少)不会将链接标记为 :visited 如果它触发 onclick 处理程序而不遵循 href.我正在使用 onclick 从服务器获取数据并修改页面,链接样式在这里似乎很合适.但该链接并未标记为已访问.
FF2 (at least) doesn't mark as link as :visited if it triggers the onclick handler without following the href. I'm using onclick to fetch data from a server and modify the page and the link styling seems appropriate here. But the link is not marked as visited.
是否有跨浏览器的方式将链接标记为已访问?如果没有,有没有办法确定浏览器的 a: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?
感谢所有回复的人.
看起来答案是:
- 是否可以跨浏览器将链接标记为已访问?
不,没有办法做到这一点.如果 href 在浏览器历史记录中,则链接被标识为已访问. - 有没有办法确定浏览器的 a:visited 样式?
不,不能单独通过 javascript.
- 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. - Is there a way to determine the browser's a:visited styling?
No, not via javascript alone.
推荐答案
这是我的做法.仅适用于支持 HTML5 历史 API 的浏览器.
Here is how I did it. Only works in browsers that support 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)
使用 replaceState 意味着后退按钮不会受到影响.
Using replaceState means that the back button won't be affected.
这篇关于使用 javascript 将链接标记为已访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!