Domanda

I was wondering if it is possible through htaccess or somehow else (but NOT JS) to make all external links (links that are not domain related) to open in a new tab (target="_blank").

Is this even possible?

Thank you!

È stato utile?

Soluzione

Any link that you generate in your page (I'm assuming you are generating the page with PHP), just do

if (strpos($link, 'yourdomain.com') === false)
{
  //append your target="_blank" to the link here  
} 

Then you're searching the link for your domain and if it is not on your domain, then making it open in a new tab.

See http://us1.php.net/strpos

Altri suggerimenti

there are only 3 ways to decide this :

  • target-attribute
  • JS
  • Browser-Settings or Plugins (depends on what browser you use, most tend to use JS)

If you don't want to use JS, then you are pretty much only left with target. You could insert it "automatically" by PHP/Ruby/Python/Java-Code (whatever you use to generate your HTML), by using search and replace functions. If you write your HTML yourself then you can set it for each link by hand.

I see no reason why you would need more options, but if you do: you're fucked.

Browsers don't get to see .htaccess and your server only delivers HTML-Files. It has no control about how they are processed. The browser decides this on it's own (this is where you could suggest to all your users to install a plugin to do this).

CSS3 property is there.

a
{
target-name:new;
target-new:tab;
} 

But unfortunately, it's not supported by any browser.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top