Question : Opening a New Window Based on URL

I have been using the JavaScript below to help open a new window if the link does not match www.example.com and has been working fine.

I guess my question is two parts - is it possible to make it open a new tab instead of a new instance?

And if I have a couple of domain names on their (i.e. www.example.com, www.example1.com, www.example2.com) which all still point to www.example.com - can I add www.example1.com, www.example2.com and www.example3.com to the code so that links that point to these specific domain names will open in the same window?

Thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
  if(!anchors[i].href.match('www.example.com/'))
    {anchors[i].target = "_blank";}
        }
}
window.onload = externalLinks;

Answer : Opening a New Window Based on URL

Yes, you can do this:

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
  if(!anchors[i].href.match('www.example.com/'))
    {anchors[i].target = "_blank";}
  if(!anchors[i].href.match('www.example1.com/'))
    {anchors[i].target = "_blank";}
  if(!anchors[i].href.match('www.example2.com/'))
    {anchors[i].target = "_blank";}
  if(!anchors[i].href.match('www.example3.com/'))
    {anchors[i].target = "_blank";}
        }
}
window.onload = externalLinks;


or

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
  if(!anchors[i].href.search('www.example') > -1)
    {anchors[i].target = "_blank";}
        }
}
window.onload = externalLinks;


Random Solutions  
 
programming4us programming4us