Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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.
0
bertstevens
Asked:
bertstevens
  • 4
  • 3
1 Solution
 
DarixCommented:
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.
0
 
bertstevensAuthor Commented:
OK, what php line for detection should i then add and what lines to change the target per link?
0
 
DarixCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bertstevensAuthor Commented:
they have different urls and servers. I increased point value 75->150  for complete code sample incl changing traget for example link.
0
 
DarixCommented:
<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.
0
 
bertstevensAuthor Commented:
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

}
0
 
eeBlueShadowCommented:
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();?>">
0
 
DarixCommented:
and more better solution:

put this in the top of include file
<?php

    if ($_SERVER["SERVER_NAME"] == "your_first_server")
         $target = "_blank";
    else
         $target = "_self";
?>

then each link looks like:

<a href="http://blah.blah.com/" target="<?=$target;?>">

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now