Forum
Opening external links in a new tab
Hi,
I'm trying to create a way that I can open external links in a new tab on my website.
When I add a link in the text-box there is now way to add a "target="_blank" option. I have been trying to use JQuery and have inserted the following code into my header:
<script>
jQuery(function($){
$('a[href^="https://"]')
.not('[href*="moatepetclinic.ie"]')
.attr('target','_blank');
});
</script>
Has anyone else come up against this problem and got around it?
Orla
Does your code not work? What happens?
Hi Drew,
Sorry - yeah the code doesn't work. Still opens in the same window!
Here's a link to the page I'm trying to get working. External links on right hand side: https://www.moatepetclinic.ie/petcare.php
If you open your browser console, you'll see an error that jQuery is not defined. That's because you're trying to use jQuery before it's been added to the page.
Move your code down to the bottom of the page, after the point jQuery is included.
Ah that's what's going on - thanks for pointing that out. It's all working perfectly now ;)
Cheers, Orla