0

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.

5
  • Do you mean you don't want to do this with target="_blank" ? Commented Mar 31, 2009 at 12:50
  • And do you also mean you want to know if there is a non-Javascript solution? Commented Mar 31, 2009 at 13:02
  • 1
    PHP is not the issue here. PHP outputs HTML and once it is on the client's screen it is a HTML issue. Commented Mar 31, 2009 at 13:05
  • "I dont want to do this without..." and "I can do this through X but want to know if there is an X solution" are really idiotic sentences. Back to grammar school, dude. Commented Mar 31, 2009 at 14:37
  • Beside the technical issue, there are usability issues: I hate when a server decides for me that links should be opened in a new window (I can do that myself, thanks), I hate even more links needing JS to open when not needed, and users can have JS disabled anyway. Commented Mar 31, 2009 at 15:16

3 Answers 3

3

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;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

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;"

3 Comments

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.
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'); }); } });
What's with all these web developers who don't understand the difference between server-side and client-side code? I'm seeing it all the time on SO, and it's dispiriting.
1

Base target should do it for you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.