Quick way to set external links to open in new tab?
-
- Sr. Propeller Head
- Posts: 125
- Joined: Thu Mar 05, 2015 4:24 am
- Location: Chesterfield, Derbyshire, UK
- Contact:
Quick way to set external links to open in new tab?
Does anyone know of a quick way to set projects to open external links (not internal) in a new tab? I tried using a script I found online, but it isn't having any effect.
Re: Quick way to set external links to open in new tab?
If 'external' means any links that aren't relative, then try this jQuery:
Code: Select all
$("a[href^='http://']").attr("target","_blank");
-
- Sr. Propeller Head
- Posts: 125
- Joined: Thu Mar 05, 2015 4:24 am
- Location: Chesterfield, Derbyshire, UK
- Contact:
Re: Quick way to set external links to open in new tab?
Thanks Dave, you're a star. That looks quite similar to the script I had actually, but slightly different. I'll give it a go.
Re: Quick way to set external links to open in new tab?
Thank you both!
I've tried to use your script, but it didn't work for me until I modified the attribute as follows (I've also adapted the script to be used with https links):
<script type="text/javascript">/<![CDATA[/
$("a[href^='https://']").attr('target="_blank"');/]]>/</script>
Hope this helps
I've tried to use your script, but it didn't work for me until I modified the attribute as follows (I've also adapted the script to be used with https links):
<script type="text/javascript">/<![CDATA[/
$("a[href^='https://']").attr('target="_blank"');/]]>/</script>
Hope this helps
Re: Quick way to set external links to open in new tab?
Yep, my code wouldn't work with https, so matching just 'http' might be a better solution, so it covers both http/https.
The attribute function works fine using either .attr("target","_blank") or .attr('target="_blank"')
Code: Select all
$("a[href^='http']").attr("target","_blank");