Jquery : Append querystring to all links

前端 未结 2 1517
轻奢々
轻奢々 2020-12-08 21:53

I would like to append a query string onto all dynamic links within a page - to fix a bug in a old release - is this possible?

Any ideas?

2条回答
  •  春和景丽
    2020-12-08 22:20

    This solution with native javascript :

    var querystring = 'yourQueryStringHere=;-)';
    
    document.addEventListener('click', function (e) {
    
        var x = e.originalTarget;
        if (x.nodeName === 'A') {
    
            var href = x.getAttribute('href');
    
            if(href) {
                href += (/\?/.test(href) ? '&' : '?') + querystring;
                x.setAttribute('href', href);
            }
        }
    
    }, false);
    

提交回复
热议问题