Jump to content

Javascript detect if links are external

So I've been using

window.onload = function(){  var anchors = document.getElementsByTagName('a');  for (var i=0; i<anchors.length; i++){    anchors[i].setAttribute('target', '_blank');  }}

To set all links to target _blank so that they open in a new tab, but I realized right before I commited it that it will do that for stuff like links to articles on my site which I don't want. Could someone help me out by modifying this to only add _target blank to external urls (distinguished by "http://" or "htpps://")?

 

Link to comment
https://linustechtips.com/topic/483156-javascript-detect-if-links-are-external/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×