Link to home
Start Free TrialLog in
Avatar of bertstevens
bertstevens

asked on

dynamically change link target property

I have to sites A and B

A has a php with a list of links that is used (included) on both A and B

What i want to achief is:
- clicking on one of those links on site A opens the link in the CURRENT browser window
- clicking on one of those links on site B opens the link in the NEW browser window

Both sites use (different) stylesheets. I was hoping to solve this with the stylesheets and/or javascript and not php.
Avatar of Darix
Darix

with css you wouldn't solve this, with javascript it is possible, but please, explain why it is not good to use <A href="" target="_self"> and <A href="" target="_blank">. if the same php file is included, you can simply detect from witcha site it was included and depending on that create target attribute.
Avatar of bertstevens

ASKER

OK, what php line for detection should i then add and what lines to change the target per link?
it depends how those sites are different. if php script included only from different php file you can detect it with _SERVER["PHP_SELF"] variable, when differs only host name _SERVER["SERVER_NAME"] variable and so on.
they have different urls and servers. I increased point value 75->150  for complete code sample incl changing traget for example link.
<A href="http://some_server/some_link.php" target="<?php
if (_SERVER["SERVER_NAME"] == "first_server") {
     echo "_blank";
} else {
     echo "_self";
}
?>">Your link</A>


if that was you needed.
The would quite ugly with 10 or more links, for the points translate this psuedocode to php:

link[1]="http://server.com/link.php";
text[1]="linktext"

...

link[10]="http://server.com/link.php";
text[10]="linktext"


for i=1 to link.length {

do here the merging and echo of the html

}
You don't even need that.

put this anywhere in your PHP file:

<?php
function getTarget()
{
    if ($_SERVER["SERVER_NAME"] == "your_first_server")
         return "_blank";
    else
         return "_self";
}
?>

then each link looks like

<a href="http://blah.blah.com/" target="<?=getTarget();?>">
ASKER CERTIFIED SOLUTION
Avatar of Darix
Darix

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial