如何使用Javascript或任何HTML通过另一个链接重定向网页上的每个链接? [英] How to Redirect each link on the webpage through another link using Javascript or any html?
问题描述
如何使用JavaScript或任何HTML通过其他链接重定向网页上的每个链接?
How to Redirect each link on the webpage through another link using JavaScript or any html?
我有一个网站 - www.example.com
I have a website - www.example.com
我在每个页面上都有几个链接,例如 -
I have few links on each page like -
- www.link1.com
- www.link2.com
- www.link3.com
我希望可以添加到每个网页的javascript并给出以下结果 -
I want the javascript that can be added to each webpage and will give the below results -
每当有人点击链接时 - 它应该显示这样的链接 - www .example.com / redirect?= www.link1.com
whenever someone clicks on the links - it should show a link like this - www.example.com/redirect?=www.link1.com
现在这个重定向脚本应该根据类似的链接来计算重定向路径,并且每当用户点击它们时它应该如下所示 -
Now this redirect script should calculate the redirect path based on links like and whenever some user clicks on them it should go like below-
link1应重定向如下 -
www.affiliate1.com/r/www.link1.com
link1 should be redirected as below - www.affiliate1.com/r/www.link1.com
和link2应如下所示 -
www.affiliate2.com/r/www.link2.com
and link2 should be like below - www.affiliate2.com/r/www.link2.com
请告知要做到这一点。我不是代码专家,但我有很少的网站,他们正在这样做。
Please advise on ow to achieve this. I am not a code expert but I have few websites where they are doing it.
推荐答案
var remember = [].slice;
var to = 'a';
var post = document.getElementsByTagName(to);
var relevant = remember.call(post);
var code = 'https://example.com?redirect=';
relevant.forEach(function(atag) {
var href = atag.href;
if ( href ) {
atag.href = code + encodeURIComponent(href);
}
});
这篇关于如何使用Javascript或任何HTML通过另一个链接重定向网页上的每个链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!