Solved

dynamically change link target property

Posted on 2004-05-02
9
248 Views
Last Modified: 2007-12-19
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
Comment
Question by:bertstevens
  • 4
  • 3
9 Comments
 
LVL 1

Expert Comment

by:Darix
ID: 10972113
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
 

Author Comment

by:bertstevens
ID: 10972153
OK, what php line for detection should i then add and what lines to change the target per link?
0
 
LVL 1

Expert Comment

by:Darix
ID: 10972409
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:bertstevens
ID: 10972466
they have different urls and servers. I increased point value 75->150  for complete code sample incl changing traget for example link.
0
 
LVL 1

Expert Comment

by:Darix
ID: 10972735
<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
 

Author Comment

by:bertstevens
ID: 10972811
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
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 10974029
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
 
LVL 1

Accepted Solution

by:
Darix earned 150 total points
ID: 10974967
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question