0

I have a website with many links on it at the moment. So I wanted to ask if it is possible to add additionally to every "normal" link an extra link (for example www.google.com)?

I know this option:

<a href="http://virtual-doctor.net" onclick="window.open('http://runningrss.com');
return true;">multiopen</a>

But then I need to edit every single link on my website. So is there a possibility to add a piece of code to the site and then every link on the website opens automatically (additionally to the normal linked page) an extra page?

I hope it's understandable what I mean :)

Greetings Andrew

Andrew
  • 3
  • 1

1 Answers1

0

If I understand you well, you want to add a click event to all your links? A bit weird request... but here is how you could do it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>

<a href="https://www.google.com">www.google.com</a>
<a href="https://www.yahoo.com">www.yahoo.com</a>
<a href="https://www.gmail.com">www.gmail.com</a>

<script>

    function openWindow(e){
        window.open('http://runningrss.com');
    }
    (function(){

    var allAnchors = document.getElementsByTagName('a');
    for (var i=0; i<allAnchors.length; i++)
    {
     var anchor = allAnchors[i]
     //First remove existing in case already registered
     anchor.removeEventListener('click',openWindow,false);
      anchor.addEventListener('click',openWindow,false);
     }
    }())
</script>

</body>
</html>    
CodeHacker
  • 2,127
  • 1
  • 22
  • 35
  • Thank you so much you safed my day! Is there a posibility that the "http://runningrss.com" site is opening in the background? Thank you so much for your help! – Andrew Sep 04 '17 at 12:27
  • I don't know what you mean with "in the background.." – CodeHacker Sep 04 '17 at 13:10
  • Now if you press the link you switch to the runninrss.com site. But is it possible that this site opens in the background and you stay on the site where the normal link directed to? – Andrew Sep 04 '17 at 13:20
  • Tht I don't know.. add another question for that or try to find the answer... For example https://stackoverflow.com/questions/6897430/prevent-window-open-from-focusing – CodeHacker Sep 04 '17 at 14:09
  • So I did some research and I think it's something with "myWindow.blur();" or "focus" so the focus stays on the original page. – Andrew Sep 04 '17 at 14:14