Solved

dynamically change link target property

Posted on 2004-05-02
9
246 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:bertstevens
Comment Utility
they have different urls and servers. I increased point value 75->150  for complete code sample incl changing traget for example link.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:Darix
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This video teaches users how to migrate an existing Wordpress website to a new domain.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now