Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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);
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...