views:

254

answers:

2

I want to open All external link into new window/tab through php without touching every external link code. and i don't want to this without target="blank".

I can do this through javascript but i want to know if there is a PHP solution.

+1  A: 

Not sure if I got this one right, but if you're looking for a JS alternative to "target=blank" then this one works and is xhtml valid:

onclick="window.open(this.href, '_blank'); return false;"

Andrei Serdeliuc
apikot thx for quick reply. I know abou javascript method but wanted to know if there any PHP method and i don't want to edit every external link manually . If i have a 100 links on a page then i need a one script which can scan all external link and open into new window if clicked.
metal-gear-solid
You'd do this via JS as well, here is a short snippet using mootools:$$('a').each(function(el){ if(el.rel == 'external') { el.addEvent('click', function(e) { e = new Event(e).stop; window.open(el.href, '_blank'); }); }});
Andrei Serdeliuc
+3  A: 

This job cannot be done with PHP. PHP is on the server side while your problem requires interaction with the client. This is a classical thing you'd use javascript for.

In case you use JQuery things become extremely simple:

// pretend you have links in your page <a href="link.htm" rel="external">Link</a>
// please note that the rel-value can be chosen at will
$(document).ready(function(){
    $('a[rel="external"]').click(function() {
        window.open(this.href, '_blank');
        return false;
    });
});
Stefan Gehrig