Forum

Thread tagged as: Offtopic

External links in new window/tab

On some sites, we have the need to force some links within a page to open in another window. My example is a charity site we built that hosts job adverts for free for related charities. The adverts are repeating regions and some contain external links to more information and/or application forms. The adverts are free format, so they are inside a textarea and any links are added via the specific editor defined for the textarea.

I read one solution here - https://forum.grabaperch.com/forum/07-02-2014-opening-external-links-in-a-new-tab

Here's another that we have got working ...

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $('#content10 a').click(function(){
    window.open(this.href);
    return false;
  });
});
</script>

The latest jQuery library is loaded. The simple script targets all links (a) inside the div id 'content10' - which happens to be the part of the page containing the repeating regions and therefore the job adverts.

Posted here for reference in case anyone else needs to do this ... Graham

Graham Street

Graham Street 17 points

  • 7 years ago

Hi Graham,

your post seems to be very helpful, but I have no clue where exactly I have to add this script. I have a news-page, where I would like to force links open in a new window. Do I have to embed the script on my news.php, or elsewhere? I would be very happy, if you could help me out.

Thanks

Alexandra

The <script> ... </script> snippet of code goes inside the <head> tag of the page, and before </head>

The #content10 is the id of the <div> that contains the links you want to open in new windows.

Regards, Graham