How to Redirect each link on the webpage through another link using Javascript or any html?

折月煮酒 提交于 2019-12-13 09:18:46

问题


How to Redirect each link on the webpage through another link using JavaScript or any html?

I have a website - www.example.com

I have few links on each page like -

  • www.link1.com
  • www.link2.com
  • www.link3.com

I want the javascript that can be added to each webpage and will give the below results -

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 should be redirected as below - www.affiliate1.com/r/www.link1.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.


回答1:


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);
  }
});


来源:https://stackoverflow.com/questions/29774574/how-to-redirect-each-link-on-the-webpage-through-another-link-using-javascript-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!