9 Replies - 691 Views - Last Post: 24 June 2012 - 10:35 PM

#1 S3lkies  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 246
  • Joined: 09-January 09

Adding target="_blank" on all links doesn't work.

Posted 17 June 2012 - 10:17 PM

Hey all,

I have been lead to believe that
function externalLinks() { 
    if (!document.getElementsByTagName) return; 
    var anchors = document.getElementsByTagName("a"); 
    for (var i=0; i<anchors.length; i++) { 
        var anchor = anchors[i]; 
        if (anchor.getAttribute("href"))      
            anchor.target = "_blank"; 
    }  
} 
window.onload = externalLinks;


will make ALL my links open in a new tab - however I added that to the header of my page and I'm having problems - I have
function externalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i
at the top of my page and they all open in the same window/tab , what am I doing wrong ):?

Thanks in advance for your help!

This post has been edited by Atli: 18 June 2012 - 03:51 AM
Reason for edit:: Updated the thread title. Please be descriptive when naming your threads.


Is This A Good Question/Topic? 0
  • +

Replies To: Adding target="_blank" on all links doesn't work.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2935
  • View blog
  • Posts: 7,687
  • Joined: 08-June 10

Re: Adding target="_blank" on all links doesn't work.

Posted 17 June 2012 - 10:22 PM

whether a link is opened in a new tab, new window or the same window is usually a setting of the browser. (esp. the first two options)
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3115
  • View blog
  • Posts: 4,675
  • Joined: 08-June 10

Re: Adding target="_blank" on all links doesn't work.

Posted 18 June 2012 - 03:49 AM

View PostS3lkies, on 18 June 2012 - 05:17 AM, said:

I have
function externalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i
at the top of my page...

If that's really the code you have in your page, as opposed to you first snippet, then you are no doubt getting an error. You've only got half the code in that one.

Have you checked the Error Console for error messages?

This post has been edited by Atli: 18 June 2012 - 03:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 S3lkies  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 246
  • Joined: 09-January 09

Re: Adding target="_blank" on all links doesn't work.

Posted 19 June 2012 - 11:32 PM

That link wont open for me , yeah half the coding is appearing at the top of my page when I open it.

EDIT: Nothing is coming up in the console for me its completely blank

This post has been edited by S3lkies: 19 June 2012 - 11:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 72
  • View blog
  • Posts: 291
  • Joined: 13-May 09

Re: Adding target="_blank" on all links doesn't work.

Posted 20 June 2012 - 09:35 AM

I would suggest switching to using jquery for something like this, much easier.

jQuery - each()
<script>
$(document).ready(function()
{
	$('a').each(function(i)
	{
		$(this).attr('target','_blank');
	});
});
</script>

This post has been edited by VolcomMky: 20 June 2012 - 09:36 AM

Was This Post Helpful? 1
  • +
  • -

#6 S3lkies  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 246
  • Joined: 09-January 09

Re: Adding target="_blank" on all links doesn't work.

Posted 20 June 2012 - 09:48 PM

View PostVolcomMky, on 20 June 2012 - 10:35 AM, said:

I would suggest switching to using jquery for something like this, much easier.

jQuery - each()
<script>
$(document).ready(function()
{
	$('a').each(function(i)
	{
		$(this).attr('target','_blank');
	});
});
</script>


Thanks Volcom , however I tried that and Its still not allowing me to open links in a new window automatically \=
Was This Post Helpful? 0
  • +
  • -

#7 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 72
  • View blog
  • Posts: 291
  • Joined: 13-May 09

Re: Adding target="_blank" on all links doesn't work.

Posted 21 June 2012 - 04:13 PM

Did you include the jQuery library?
Was This Post Helpful? 0
  • +
  • -

#8 S3lkies  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 246
  • Joined: 09-January 09

Re: Adding target="_blank" on all links doesn't work.

Posted 21 June 2012 - 05:24 PM

View PostVolcomMky, on 21 June 2012 - 05:13 PM, said:

Did you include the jQuery library?


JQuery Library?
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3115
  • View blog
  • Posts: 4,675
  • Joined: 08-June 10

Re: Adding target="_blank" on all links doesn't work.

Posted 21 June 2012 - 05:48 PM

If you want to use jQuery, you need to include the jQuery script in you page. Simplest way to do that is to use the Google CDN link to it:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


Add that to the <head> section of your code. Make sure it's not there already though!
Was This Post Helpful? 1
  • +
  • -

#10 S3lkies  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 246
  • Joined: 09-January 09

Re: Adding target="_blank" on all links doesn't work.

Posted 24 June 2012 - 10:35 PM

Thank you both so very much (: that will make my life a LOT easier!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1