using javascript to mark a link as visited

前端 未结 5 1561
孤独总比滥情好
孤独总比滥情好 2020-12-06 01:17

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 th

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 01:44

    Is there a way to determine the browser's a:visited styling?

    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

提交回复
热议问题