Javascript 如何重定向除社交链接外的外部链接页面

Javascript 如何重定向除社交链接外的外部链接页面,javascript,http-redirect,Javascript,Http Redirect,我有一个博客,我想通过使用javascript为所有链接(内部链接和社交链接除外)创建重定向页面来跟踪外部链接。 我知道如何这样做: window.onload = function(){ document.getElementById("aLinkId").addEventListener("click", function(e){ e.preventDefault(); // Here we redirect to the wanted page, with the extra param


我有一个博客,我想通过使用javascript为所有链接(内部链接和社交链接除外)创建重定向页面来跟踪外部链接。
我知道如何这样做:

window.onload = function(){
document.getElementById("aLinkId").addEventListener("click", function(e){
e.preventDefault();
// Here we redirect to the wanted page, with the extra parameter, with the original external URL
location.href = "REDIRECT-PAGE-URL?redirect=" + document.getElementById("aLinkId").href; 
}); };
但这在博客链接中不起作用。如果没有id,我必须为每个链接都添加id。

但是对于重定向页面,我被卡住了。

您可以使用获取所有链接,然后循环遍历每个链接并在其上添加事件侦听器。这种方式并不依赖于每个链接都有唯一的ID

window.onload=function(){
让links=document.queryselectoral(“a”);
对于(变量i=0;i

博客链接是什么样子的?您可以使用比id更通用的选择器来获取链接并在它们之间循环。只需一个没有id的链接“”